首页 文章

Unicode写入失败:charmap无法编码字符

提问于
浏览
-1

我有一个文本,其中我有一个摘要 . 我用一些正则表达式来获取这个摘要,因为文本总是具有相同的结构 .
在摘要中有一个句子"NAME is classified as ....",我必须用文本中抓取的 Headers 替换它,并用逗号分隔的word1和word2组成 . 只要我这样做,它工作正常(因此我赢了't add the full code because it is very large and I can' t),无论如何问题不在我将提供的范围之外 .
我需要根据word1添加unicode字符\ u2191或\ u2193,它与字典中的正值或负值相关联 . 这必须在替换句子之前完成 . 我的代码基本上如下:

import re
import io
file=open(Summaries_file,'a')#also tried open(Summaries_file,'a', encoding="UTF_16_LE") and file=io.open(Summaries_file,'a', encoding="UTF_16_LE")
code_dict["page"]="Word1\u2191"
page="page"
summary = "Data is: 111919919. Name is classified as an infered value".
print(summary)
#OUTPUT>"Data is: 111919919. Name is classified as an infered value".
title= "Word1, Word2"

#this is the part added to regular code>>>>  

titlelist=title.split(",")
if titlelist[0]==code_dict[page]:
    titlelist[0]=code_dict[page]+"\u2191"
    title=str(titlelist)
    print(titlelist[0])
    #OUTPUT>"Word1↑"#It displays the arrow well
    print(title) #ok, too.
    #OUTPUT>"Word1↑, Word2"

 #We go back to the end of the normal code
insert=re.compile("is classified as")
print(type(summary))
#<class 'str'>
summary=str(insert.sub(title, summary))
print(summary)
#OUTPUT>"Data is: 111919919. Name Word1↑, Word2 an infered value".

print("passed")
file.write(title+'\n')
file.write(summary+'\n')

然后跟踪(最近的呼叫最后):

File "<ipython-input-1-6bc913872cc9>", line 1, in <module>
runfile('C:/Python Scripts/txtad.py', wdir='C:/Users/Laurent/Documents/Python Scripts')

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)

File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

File "C:/Python Scripts/txtad", line 380, in <module>
file.write(title+'\n')

File "C:\Anaconda3\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode character '\u2191' in position 11: character maps to <undefined>

现在,我无法弄明白,我严重坚持这一点 .
我不知道为什么它首先写入失败,因为它很好地显示了标志,并且我在某些测试中明确地编码到正确的系统,甚至用正确的编码打开文件 .

我尝试了各种你可以在那里阅读的东西:

https://stackoverflow.com/questions/43706177/solving-error-when-adding-an-unicode-character-to-splits-of-a-string-then-revert?noredirect=1#comment74463879_43706177

实际上原始代码更大,但我尝试了这个并且它以相同的方式工作,并且输入类型严格相同 .

我看了这些:
UnicodeEncodeError: 'charmap' codec can't encode character '\u2010': character maps to <undefined>
UnicodeEncodeError: 'charmap' codec can't encode characters
Python, Unicode, and the Windows console
python 3.2 UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 9629: character maps to <undefined>
最后,它们比其他任何东西都更令人困惑 .

无论如何问题不在于控制台就像那些其他帖子一样,因为问题出现在没有显示的写入指令上,此外,这个角色在我的控制台上很好地显示...
我真的可以't tell what'继续以及如何管理这个问题 .
感谢您的见解 .

1 回答

  • 0

    我终于通过阅读本文和链接文章以及TadhgMcDonald-Jensen的评论解决了这个问题 . Writing Unicode text to a text file?

    实际上我只需要在写入每个字符串时打开(文件,“wb”,)和编码(因为它们不是字节) . 我想我可以使用io或编解码器导入并使用向后兼容性打开 .

相关问题