是用python處理excel過程中,從表格中解析除字符串,打印出來的中文卻顯示成了u'開頭的亂碼字符串,在控制台中輸出的編碼格式是utf-8,而excel表格的數據也是utf-8編碼成的,但是解析成字符串則是成了一個unicode編碼組成的字符串,“\u”后的16進制字符串是相應漢字的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()