简体   繁体   English

UnicodeEncodeError: 'charmap' 编解码器无法编码字符/写入 txt 文件

[英]UnicodeEncodeError: 'charmap' codec can't encode characters/ writing in txt file

i'm parcing a text file that has text in xml like configuration and the code i tried is this我正在解析一个文本文件,其中包含 xml 中的文本,例如配置,我尝试的代码是这样的

file_handle_tester = open("C:/Users/pc/Desktop/talabat yarmook.txt","r", encoding="utf8")


sec_file = open("C:/Users/pc/Desktop/parced_text.txt","w")
a='com.talabat:id/textView_restaurantName'
menu = list()

for line in file_handle_tester:
    line = line.strip()
    menu.append(line)

for line in menu:
    sec_file.write(line)

python is not letting me print lines from the original file to the new file and i get this error: python 不允许我将原始文件中的行打印到新文件中,我收到此错误:

Traceback (most recent call last):
  File "C:\Users\pc\Desktop\pyAppiumSandBox\venv\parcing_handle.py", line 14, in <module>
    sec_file.write(line)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python311\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 95-101: character maps to <undefined>

in the code above i tried to put the lines in a list because python has no problem with printing them on screen.在上面的代码中,我试图将这些行放在一个列表中,因为 python 在屏幕上打印它们没有问题。 the entire issue is when writing them.整个问题是在编写它们时。 but i still get the same error.但我仍然遇到同样的错误。

i tried opening the txt in byte format and decode it but that didn't work also我尝试以字节格式打开 txt 并对其进行解码,但这也不起作用

Simply change sec_file = open("C:/Users/pc/Desktop/parced_text.txt","w") to只需将sec_file = open("C:/Users/pc/Desktop/parced_text.txt","w")更改为

sec_file = open("C:/Users/pc/Desktop/parced_text.txt","w", encoding='utf-8')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnicodeEncodeError: 'charmap' 编解码器无法编码字符 - UnicodeEncodeError: 'charmap' codec can't encode characters CSV模块触发“ UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符” - “UnicodeEncodeError: 'charmap' codec can't encode characters” triggered by CSV module Python,UnicodeEncodeError:“ charmap”编解码器无法在位置编码字符 - Python, UnicodeEncodeError: 'charmap' codec can't encode characters in position Python Dataframe 到 CSV - UnicodeEncodeError: 'charmap' 编解码器无法编码字符 - Python Dataframe to CSV - UnicodeEncodeError: 'charmap' codec can't encode characters 将结果从 python 写入 csv 文件 [UnicodeEncodeError: &#39;charmap&#39; codec can&#39;t encode character - Writing out results from python to csv file [UnicodeEncodeError: 'charmap' codec can't encode character UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符 - UnicodeEncodeError: 'charmap' codec can't encode character python错误codecs.charmap_encode(input,errors,encoding_map)UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符 - python error codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode characters 使用 Webscraper 写入 csv 时出现“UnicodeEncodeError: &#39;charmap&#39; codec can&#39;t encode character” - “UnicodeEncodeError: 'charmap' codec can't encode character” When Writing to csv Using a Webscraper Python 3-UnicodeEncodeError:“ charmap”编解码器无法编码字符(编码后在文件中) - python 3 - UnicodeEncodeError: 'charmap' codec can't encode character (Encode so it's in a file) UnicodeEncodeError: &#39;charmap&#39; 编解码器无法对位置 1082-1​​084 中的字符进行编码:字符映射到<undefined> - UnicodeEncodeError: 'charmap' codec can't encode characters in position 1082-1084: character maps to <undefined>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM