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

-1 投票
1 回答
1615 浏览
提问于 2025-05-16 19:50

我有一段文字,其中包含一个摘要。我通过一些正则表达式提取这个摘要,因为这段文字的结构总是一样的。
在摘要中,有一句话是“NAME被分类为....”,我需要把它替换成一个标题,这个标题是从文本中提取的,由word1和word2组成,中间用逗号隔开。只要我这样做,就没问题(所以我不会把完整的代码贴出来,因为它很长,我也不能这样做,反正问题不在我提供的内容之外)。
我需要在替换这句话之前,给它添加一个unicode字符\u2191或\u2193,这取决于word1,这个字符在一个字典中与正值或负值相关联。我的代码基本上是这样的:

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>
这些内容最终让我更困惑。

无论如何,问题不在控制台,因为问题出现在写入指令上,而不是显示的内容,而且这个字符在我的控制台上显示得很好...
我真的不知道发生了什么,也不知道该如何解决这个问题。
谢谢你的见解。

相关问题:

  • 暂无相关问题
暂无标签

1 个回答

0

我终于通过阅读这篇文章和相关链接的内容,以及TadhgMcDonald-Jensen的评论,解决了这个问题;如何将Unicode文本写入文本文件?

其实我只需要用“wb”模式打开文件,然后在写入每个字符串的时候指定编码(因为它们不是字节)。我想我也可以使用io或codecs模块来打开文件,以保持向后兼容。

撰写回答