如何在python中读取带有特殊字符的文本文件

问题描述 投票:0回答:1

我正在尝试使用特殊字符读取文本文件,例如:الحمدللهربالعالمين

我正在使用:

import fileinput 
fileToSearch = "test_encoding.txt"
with open(fileToSearch, 'r', encoding='utf-8') as file:
    counter = 0;
    for line in file:
        print(line)

但Python崩溃了这条消息:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print(line)
  File "C:\Users\atheelm\AppData\Local\Programs\Python\Python35-
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 0-1: 
character maps to <undefined>

我有Python 3.5.1,我正在使用Windows。

我正在运行此命令:

py test.py > out.txt
python utf-8 character-encoding
1个回答
0
投票

使用2个diff文件并使用io:

lines=["Init"]
with io.open(fileToSearch,'r',encoding='utf-8') as file:
    counter = 1;
    for line in file:
        lines.insert(counter,str(line))
        counter = counter+1
with io.open(out_file,'w',encoding='utf-8') as file:
    for item in lines:
        file.write("%s\n" % item)
© www.soinside.com 2019 - 2024. All rights reserved.