新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:pythonu開(kāi)頭的字符串亂碼怎么解決
python處理u開(kāi)頭的字符串

是用python處理excel過(guò)程中,從表格中解析除字符串,打印出來(lái)的中文卻顯示成了u'開(kāi)頭的亂碼字符串,在控制臺(tái)中輸出的編碼格式是
utf-8,而excel表格的數(shù)據(jù)也是utf-8編碼成的,但是解析成字符串則是成了一個(gè)unicode編碼組成的字符串,“\u”后的16進(jìn)制字符串是
相應(yīng)漢字的utf-16編碼,所以我們需要將這寫字符串解碼成unicode字符串。
使用decode("unicode_escape")
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from collections import OrderedDict
from pyexcel_xls import get_data
from pyexcel_xls import save_data
import redis
def read_xls_file():
xls_data = get_data(r"test.xlsx")
print "Get data type:", type(xls_data)
conn = redis.Redis()
for key in xls_data['sheet1']:
key = str(key).decode("unicode_escape").encode("utf8")
print key
key = key.lstrip()
key = key.rstrip()
# conn.set(key, key)
if __name__ == '__main__':
read_xls_file()推薦學(xué)習(xí)《Python教程》!
文章名稱:創(chuàng)新互聯(lián)Python教程:pythonu開(kāi)頭的字符串亂碼怎么解決
路徑分享:http://m.fisionsoft.com.cn/article/djdihhi.html


咨詢
建站咨詢
