当我在agend中写“你”并以unicode模式将其保存为test-unicode.txt时,用打开它xxd g:\\test-unicode.txt
,我得到了:
0000000: fffe 604f ..`O
1.fffe代表小尾数
2.的unicode你
是\x4f\x60
我想在文件中写你
as604f
或4f60
。
output=open("g://test-unicode.txt","wb")
str1="你"
output.write(str1)
output.close()
错误:
TypeError: 'str' does not support the buffer interface
当我将其更改为以下内容时,没有错误。
output=open("g://test-unicode.txt","wb")
str1="你"
output.write(str1.encode())
output.close()
当打开它时xxd g:\\test-unicode.txt
,我得到:
0000000: e4bd a0 ...
我怎样才能像Microsoft Aengda一样将其写入604f
或4f60
写入文件(另存为Unicode格式)?
“ Unicode”作为编码实际上是UTF-16LE。
with open("g:/test-unicode.txt", "w", encoding="utf-16le") as output:
output.write(str1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句