0

hello i want to print a Arabic text file in python3, and this is my code:

#!/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)

But i got this error:

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)

when trying this solution:

#!/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)

I got this error:

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:

مرحبا
مرحباً بك
السلام عليكم
وعليكم السلام
ماهو سعر الايفون٧؟
يأتي بسعتين ٣٢ قيقا ب ٢٤٩٩ ريال  و ١٢٨ قيقا ٢٨٩٩ ريال

when run locale :

LANG=
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

when run locale charmap :

US-ASCII
Eman Saad
  • 19
  • 7

2 Answers2

0

use arabic_reshaper

import arabic_reshaper

text_to_be_reshaped = 'اللغة العربية رائعة'
reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped)
display_text = get_display(reshaped_text) #### important
print(display_text)

https://pypi.org/project/arabic-reshaper/

-1

You can do this:

text = "مرحبا "

print(text.encode('utf8'))

check this tutorial Arabic in python

youssef
  • 65
  • 1
  • 8
  • 1
    i got this output : b'\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7 ' and when i removed the ".encode('utf8')" it goes back to the error in the question – Eman Saad Oct 04 '17 at 17:06
  • i saw in another question here that it could be that my locale and/or environment is broken so when i ran local i got this : LANG= LC_COLLATE="C" LC_CTYPE="C" LC_MESSAGES="C" LC_MONETARY="C" LC_NUMERIC="C" LC_TIME="C" LC_ALL= – Eman Saad Oct 04 '17 at 17:29