简体   繁体   English

UnicodeEncodeError: &#39;charmap&#39; 编解码器无法对位置 1082-1​​084 中的字符进行编码:字符映射到<undefined>

[英]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.

相关问题 Python-UnicodeEncodeError:“ charmap”编解码器无法对位置85-89中的字符进行编码:字符映射到<undefined> - Python - UnicodeEncodeError: 'charmap' codec can't encode characters in position 85-89: character maps to <undefined> UnicodeEncodeError: &#39;charmap&#39; 编解码器无法对位置 131-132 中的字符进行编码:字符映射到<undefined> - UnicodeEncodeError: 'charmap' codec can't encode characters in position 131-132: character maps to <undefined> 获取:UnicodeEncodeError:&#39;charmap&#39; 编解码器无法对位置 0-1 中的字符进行编码:字符映射到<undefined> - Getting:UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-1: character maps to <undefined> UnicodeEncodeError: &#39;charmap&#39; 编解码器无法编码字符:字符映射到<undefined> - UnicodeEncodeError: 'charmap' codec can't encode character : character maps to <undefined> UnicodeEncodeError:&#39;charmap&#39;编解码器无法对字符字符映射进行编码<undefined> - UnicodeEncodeError: 'charmap' codec can't encode character character maps to <undefined> 字符编码错误:UnicodeEncodeError:&#39;charmap&#39;编解码器无法对位置Y中的字符X进行编码:字符映射到<undefined> - Character encoding error: UnicodeEncodeError: 'charmap' codec can't encode character X in position Y: character maps to <undefined> UnicodeEncodeError:&#39;charmap&#39;编解码器无法对位置0中的字符&#39;\\ x80&#39;进行编码:字符映射到<undefined> - UnicodeEncodeError : 'charmap' codec can't encode character '\x80' in position 0 : character maps to <undefined> UnicodeEncodeError:&#39;charmap&#39;编解码器无法在位置206中编码字符&#39;\\ x97&#39;:字符映射到<undefined> - UnicodeEncodeError: 'charmap' codec can't encode character '\x97' in position 206: character maps to <undefined> UnicodeEncodeError: &#39;charmap&#39; codec can&#39;t encode character &#39;\ğ&#39; in position 1087: character maps to<undefined> - UnicodeEncodeError: 'charmap' codec can't encode character '\u011f' in position 1087: character maps to <undefined> UnicodeEncodeError:'charmap' 编解码器无法在 position 102 中对字符 '\x85' 进行编码:字符映射到<undefined></undefined> - UnicodeEncodeError :'charmap' codec can't encode character '\x85' in position 102: character maps to <undefined>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM