【发布时间】:2020-03-11 07:53:23
【问题描述】:
我有一个TOML 文件,我想用this script 处理它。
这曾经在 Linux 下运行良好。在 Windows (Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:23:52) [MSC v.1900 32 bit (Intel)] on win32) 下我收到以下错误:
Need to process 1 file(s)
Processing file test01.toml (1 of 1)
Traceback (most recent call last):
File "py/process.py", line 27, in <module>
add_text_fragment(input_dir + "/" + file)
File "<string>", line 10, in add_text_fragment
File "C:\Users\1\Anaconda3\lib\site-packages\toml\decoder.py", line 134, in lo
ad
return loads(f.read(), _dict, decoder)
File "C:\Users\1\Anaconda3\lib\encodings\cp1251.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 985: char
acter maps to <undefined>
我假设错误发生在这里的某个地方:
f = open(toml_file_name, "r")
pt = toml.load(f)
f.close()
根据 NotePad++,有问题的 file 具有 UTF-8 编码。
我该如何解决?
赏金条款
我将把这个赏金奖励给一个向我展示如何确保脚本 process.py 正确处理 input file 的人,即。 e.执行通过addTextFragment.py 中以If at this point pt 开头的注释
def add_text_fragment(toml_file_name):
f = open(toml_file_name, "r")
pt = toml.load(f)
f.close()
# If at this point pt contains dthe data of the input file,
# then you have attained the goal.
if (pt["type"] == "TA"):
变量pt 包含来自input file 的数据。
您的解决方案必须在 Windows 10、Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32 下运行。
注意:process.py 对特定目录中的所有文件执行 addTextFragment.py。
【问题讨论】:
标签: python python-3.x windows character-encoding toml