这篇文章主要介绍解决python中u开头字符串乱码的方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

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-*-fromcollectionsimportOrderedDictfrompyexcel_xlsimportget_datafrompyexcel_xlsimportsave_dataimportredisdefread_xls_file():xls_data=get_data(r"test.xlsx")print"Getdatatype:",type(xls_data)conn=redis.Redis()forkeyinxls_data['sheet1']:key=str(key).decode("unicode_escape").encode("utf8")printkeykey=key.lstrip()key=key.rstrip()#conn.set(key,key)if__name__=='__main__':read_xls_file()

以上是解决python中u开头字符串乱码的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!