如何在python

时间:2017-12-04 14:38:31

标签: python utf-8 character-encoding

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

我正在使用:

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

1 个答案:

答案 0 :(得分:0)

使用2个差异文件并使用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)