python處理u開頭的字符串


是用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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM