[英]UnicodeEncodeError: 'charmap' codec can't encode characters in position 1082-1084: character maps to <undefined>
this is my first time using python, im trying to make CT asset to CSV这是我第一次使用 python,我试图将 CT 资产转换为 CSV
this is my code这是我的代码
tsv.py tsv.py
def write(self, data):
"""
Write list to TSV
:param list data: Decoded list to write on TSV file
"""
os.makedirs(os.path.dirname("./{0}".format(self.path)), exist_ok=True)
self.tsv = open(self.path, "w", newline="\r\n")
self.header = "\t".join(i for i in data[0]) + "\n"
self.types = "\t".join(i for i in data[1]) + "\n"
self.rows = "\n".join("\t".join(l) for l in data[2:]) + "\n"
self.tsv.write(self.header + self.types + self.rows)
if self.verbose:
print("\nTSV write to \"{0}\" complete!\n".format(self.path))
And I'm getting the following error:我收到以下错误:
line 48, in write
self.tsv.write(self.header + self.types + self.rows)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1082-1084: character maps to <undefined>
What can I do to fix this?我能做些什么来解决这个问题?
The error message indicates that you are using Python 3 and that it is trying to write text using the Windows code page 1252 encoding.该错误消息表明您使用的是 Python 3,并且它正在尝试使用 Windows 代码页 1252 编码编写文本。 The actual error is that your data contains a character which does not exist in this encoding.实际错误是您的数据包含此编码中不存在的字符。 I would have expected Python 3 to write UTF-8 but it's not;我原以为 Python 3 会编写 UTF-8,但事实并非如此; perhaps you can add encoding="utf-8"
to the open
call if that is in fact what you want?如果这实际上是您想要的,也许您可以将encoding="utf-8"
到open
调用中?
self.tsv = open(self.path, "w", newline="\r\n", encoding="utf-8")
If your script works reliably for you, there is no real need to change it;如果您的脚本可靠地为您工作,则没有必要对其进行更改; but reinventing CSV/TSV writing is not a good idea -- you are ignoring several corner cases which the existing csv
module in the Python standard library already handles.但是重新发明 CSV/TSV 编写并不是一个好主意——您忽略了Python 标准库中现有csv
模块已经处理的几个极端情况。
import csv
import logging
class whatever: ...
def write(self, data):
"""
Write list to TSV
:param list data: Decoded list to write on TSV file
"""
os.makedirs(os.path.dirname("./{0}".format(self.path)), exist_ok=True)
with open(self.path, "w", newline="", encoding="utf-8") as tsv:
writer = csv.writer(tsv, delimiter="\t")
writer.writerows(data)
logging.info('TSV write to "%s" complete!', self.path))
Your code also lacks self.tsv.close()
which the above code doesn't need because the with
handles that transparently.您的代码还缺少上面代码不需要的self.tsv.close()
,因为with
处理透明。
Additionally, I switched to logging
for the informational message.此外,我切换到logging
信息性消息。 It similarly offers a canned solution which already knows how to write messages to standard error instead of standard output etc (though you might want to look at logging.basicConfig
to set up the formatting to your liking -- I usually put logging.basicConfig('%(module)s:%(message)s')
for simple command-line utilities).它同样提供了一个罐头解决方案,它已经知道如何将消息写入标准错误而不是标准输出等(尽管您可能想查看logging.basicConfig
以设置您喜欢的格式——我通常把logging.basicConfig('%(module)s:%(message)s')
用于简单的命令行实用程序)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.