user20753874
user20753874

Reputation:

How to put Arabic text in a txt file

I have been trying to put Arabic text in a .txt file and when do so using the code bellow I get this error: UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-4: character maps to <undefined>

code:

Log1 = open("File.txt", "a")
Log1.write("سلام")
Log1.close()

This question was asked in stack overflow many times but all of them has suggested using utf-8 which will output \xd8\xb3\xd9\x84\xd8\xa7\xd9\x85 for this case, I was wondering if there is anyways to make the thing in the text file look like the سلام instead of \xd8\xb3\xd9\x84\xd8\xa7\xd9\x85.

Upvotes: 2

Views: 622

Answers (1)

Adon Bilivit
Adon Bilivit

Reputation: 27043

This works perfectly:

FILENAME = 'foo.txt'

with open(FILENAME, "w", encoding='utf-8') as data:
    data.write("سلام")

Then from a zsh shell:

cat foo.txt
سلام

Upvotes: 2

Related Questions