pythonarabic

use arabic-reshaperin and bidi for convert a text in py (convert from file)


i need to use reshaperin And bidi in python for convert some arabic Text

my question is how can i use arabic-reshaper for convert text inside of a txt file and save converted text in other txt file

its my code for convert one line

import arabic_reshaper
from bidi.algorithm import get_display
text = 'سلام محمد خوبی'

test = arabic_reshaper.reshape(text)
print(get_display(test))

i know its a easy question But I haven't worked with Python until today tnx


Solution

  • Possible using a list

    import arabic_reshaper
    from bidi.algorithm import get_display
    text = 'سلام محمد خوبی'
    test = arabic_reshaper.reshape(text)
    print(get_display(test))
    lines = []
    
    with open("file.txt", encoding="utf8") as file_in:
        for line in file_in:
            lines.append(arabic_reshaper.reshape(line))
    
    #Save it to file with a line break
    with open("file2.txt", "w", encoding="utf8") as output: #created if it doesnt exist
        for line in lines:
            output.write(str(line) + '\n')