究竟是一個unicode字符串?什麼是unicode字符串?
什麼是普通字符串和unicode字符串之間的區別?
什麼是UTF-8?
我想學習Python,現在,我不斷聽到這個流行語。下面的代碼是做什麼的?
I18N字符串(Unicode)的
> ustring = u'A unicode \u018e string \xf1'
> ustring
u'A unicode \u018e string \xf1'
## (ustring from above contains a unicode string)
> s = ustring.encode('utf-8')
> s
'A unicode \xc6\x8e string \xc3\xb1' ## bytes of utf-8 encoding
> t = unicode(s, 'utf-8') ## Convert bytes back to a unicode string
> t == ustring ## It's the same as the original, yay!
True
文件的Unicode
import codecs
f = codecs.open('foo.txt', 'rU', 'utf-8')
for line in f:
# here line is a *unicode* string
互聯網搜索可能是一個很好的開始.... –
可能重複的[Unicode在Python](http://stackoverflow.com/questions/8277277/unicode-in-python) – tripleee
另請參見http: //bit.ly/unipain – tripleee