尝试将符号写入 python 3.6.0 的文件,但出现错误
Trying to write a symbol to a file for python 3.6.0 but I'm getting errors
我正在尝试将符号 ฿ 添加到 Python 3.6.0 中的文本文件,但我一直收到错误消息:
UnicodeEncodeError: 'charmap' codec can't encode character '\u0e3f'
in position 0: character maps to <undefined>
我也尝试过将 f.write('฿')
更改为 f.write('฿'.encode("\u0e3f"))
,但随后收到错误消息:
LookupError: unknown encoding: ฿
我尝试了几种不同的方法,但仍然遇到错误。也许有什么我想念的吗?任何 tips/answers 将不胜感激!
你只需要设置输出文件编码:
outstring = "฿"
with open("f:/toolbuild/temp/temp.txt", "wt", encoding="utf-8") as outfile:
outfile.write(outstring)
效果很好!
我正在尝试将符号 ฿ 添加到 Python 3.6.0 中的文本文件,但我一直收到错误消息:
UnicodeEncodeError: 'charmap' codec can't encode character '\u0e3f'
in position 0: character maps to <undefined>
我也尝试过将 f.write('฿')
更改为 f.write('฿'.encode("\u0e3f"))
,但随后收到错误消息:
LookupError: unknown encoding: ฿
我尝试了几种不同的方法,但仍然遇到错误。也许有什么我想念的吗?任何 tips/answers 将不胜感激!
你只需要设置输出文件编码:
outstring = "฿"
with open("f:/toolbuild/temp/temp.txt", "wt", encoding="utf-8") as outfile:
outfile.write(outstring)
效果很好!