你好,我想打印在python3一个阿拉伯语的文本文件,这是我的代码:印刷阿拉伯文文本文件
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# encoding: utf-8
#Read from the input file
input_file = open('triggertest.txt', 'r+')
for line in input_file:
print (line)
但我得到这个错误:
Traceback (most recent call last):
File "try.py", line 6, in <module>
for line in input_file:
File "/Users/emansaad/anaconda/lib/python3.5/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd9 in position 0: ordinal not in range(128)
时试试这个解决方案:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# encoding: utf-8
#Read from the input file
input_file = open('triggertest.txt', 'r+',encoding='utf-8')
for line in input_file:
print (line)
我得到这个错误:
Traceback (most recent call last):
File "try.py", line 7, in <module>
print (line)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
triggertest.txt:
مرحبا
مرحباً بك
السلام عليكم
وعليكم السلام
ماهو سعر الايفون٧؟
يأتي بسعتين ٣٢ قيقا ب ٢٤٩٩ ريال و ١٢٨ قيقا ٢٨٩٩ ريال
运行locale
时:
LANG=
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=
当运行locale charmap
:
US-ASCII
[用波斯语/阿拉伯语字符的Python 3 print()函数(的可能的复制https://stackoverflow.com/questions/39528462/python -3-print-function-with-farsi-arabic-characters) – cosinepenguin
@cosinepenguin我厌倦了解决方案,但没有运气 –
'triggertest.txt'中的一行代码是什么? – cosinepenguin