python 中文unicode編碼
阿新 • • 發佈:2018-12-21
一、 excel中寫入中文報錯UnicodeDecodeError : ‘ascii’ codec can’t decode byte 0xe5 in position 0: ordinal not in range(128)
1.向excel中追加內容
解決方法:
- 第一行加入
# -*- coding: utf-8 -*-
, - 將中文按“Unicode”字元編碼方式進行寫入。
程式碼如下:
# -*- coding: utf-8 -*- import xlwt name = u'小明' excel = xlwt.Workbook() sheet1 = excel.add_sheet(u'sheet1', cell_overwrite_ok=True) sheet1.write(0, 0, u'你好') sheet1.write(0, 1, u'hello, ' + name) excel.save('save_filepath')
插入工作表‘sheet1’:u'sheet1'
寫入中文:u'你好'
,u'hello, ' + name
2.新建excel寫入中文
# -*- coding: utf-8 -*-
from xlwt import Workbook
book =Workbook(encoding='utf-8')
sheet1 =book.add_sheet('list')
二、unicode 與utf-8編碼
unicode->str :a.encode("utf-8")
str->unicode: unicode(b, "utf-8")
或 b.decode("utf-8")
程式碼如下:
a = u'你好'
b = a.encode("utf-8")
c = unicode(b, "utf-8")
print a, type(a)
print b, type(b)
print c, type(c)
print c + u"hahaha", type(c + u"hahaha")
輸出:
你好 <type ‘unicode’>
你好 <type ‘str’>
你好 <type ‘unicode’>
你好hahaha <type ‘unicode’>
s = u'\u4eba\u751f\u82e6\u77ed\uff0cpy\u662f\u5cb8' print s
輸出:人生苦短,py是岸
三、unicode與普通string字串相互轉換
unicodestring = u"Hello world"
1.將Unicode轉化為普通Python字串:“encode” 編碼
utf8string = unicodestring.encode("utf-8")
asciistring = unicodestring.encode("ascii")
2.將普通Python字串轉化為Unicode:“decode” 解碼
plainstring1 = unicode(utf8string, "utf-8")
plainstring2 = unicode(asciistring, "ascii")
四、str.rsplit()
str.rsplit(’.’, num1)[num2],其中num1為要將str從後向前分為num1+1部分(列表形式),num2為取第num2+1部分。
a = '20181115 15:33:58:193804_保修卡CYWK-6016S(sharp)(英文)2360.pdf.pdf'
print a.rsplit('.', 3)[0]
輸出:20181115 15:33:58:193804_保修卡CYWK-6016S(sharp)(英文)2360