本文翻译自:Unicode (UTF-8) reading and writing to files in Python
I'm having some brain failure in understanding reading and writing text to a file (Python 2.4). 我在理解文本读取和写入文件时遇到了一些大脑失败(Python 2.4)。
# The string, which has an a-acute in it.
ss = u'Capit\xe1n'
ss8 = ss.encode('utf8')
repr(ss), repr(ss8)
("u'Capit\\xe1n'", "'Capit\\xc3\\xa1n'") (“u'Capit \\ xe1n'”,“'Capit \\ xc3 \\ xa1n'”)
print ss, ss8
print >> open('f1','w'), ss8
>>> file('f1').read()
'Capit\xc3\xa1n\n'
So I type in Capit\\xc3\\xa1n
into my favorite editor, in file f2. 所以我在文件f2中输入Capit\\xc3\\xa1n
到我最喜欢的编辑器中。
Then: 然后:
>>> open('f1').read()
'Capit\xc3\xa1n\n'
>>> open('f2').read()
'Capit\\xc3\\xa1n\n'
>>> open('f1').read().decode('utf8')
u'Capit\xe1n\n'
>>> open('f2').read().decode('utf8')
u'Capit\\xc3\\xa1n\n'
What am I not understanding here? 我在这里不理解什么? Clearly there is some vital bit of magic (or good sense) that I'm missing.<