How to put Arabic text in a txt file

Question:

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 xd8xb3xd9x84xd8xa7xd9x85 for this case, I was wondering if there is anyways to make the thing in the text file look like the سلام instead of xd8xb3xd9x84xd8xa7xd9x85.

Asked By: kevinjonson133

||

Answers:

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
سلام
Answered By: Cobra

To write Arabic text to a file, you will need to use the utf-8 encoding. This is because Arabic characters are not part of the ASCII character set, which is the default character set used by the open() function in Python when no encoding is specified.

Here is an example of how you can write Arabic text to a file using the utf-8 encoding:

with open("File.txt", "a", encoding="utf-8") as Log1:
    Log1.write("سلام")

This code will write the Arabic text "سلام" to the file "File.txt" using the utf-8 encoding. This will ensure that the Arabic characters are encoded properly and can be read correctly by other programs that support the utf-8 encoding.

In general, it’s always a good idea to specify the encoding when reading or writing text to a file in Python, to ensure that the characters are encoded and decoded properly. This is especially important when dealing with characters from non-Latin scripts, such as Arabic, Chinese, or Russian.

Answered By: eatdjo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.