'How to read txt files with those languages which is read from right to left with python

Here is a code that is reading a file for me. But each word of the file is reversed from the original text that I have put in line.txt file.

line.txt

  اور ایک 

test.py

import re

f = open("line.txt",encoding="utf-8")
text = f.read()
print(text)

OUTPUT enter image description here



Solution 1:[1]

try this https://github.com/mpcabd/python-arabic-reshaper

or print(text[::-1])

Solution 2:[2]

The simple solution is to print the reversed text:

print(text[::-1])

Another solution is to use a bi-directional library python-bidi

from bidi.algorithm import get_display

f = open("line.txt",encoding="utf-8")
text = f.read()
print(get_display(text))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mbpy
Solution 2 Hamed Rabiei