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