溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

解決python中u開頭字符串亂碼的方法

發(fā)布時(shí)間:2020-07-27 11:38:12 來源:億速云 閱讀:549 作者:清晨 欄目:編程語言

這篇文章主要介紹解決python中u開頭字符串亂碼的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

python處理u開頭的字符串

是用python處理excel過程中,從表格中解析除字符串,打印出來的中文卻顯示成了u'開頭的亂碼字符串,在控制臺(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()

以上是解決python中u開頭字符串亂碼的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI