Chaim Stanton
Chaim Stanton

Reputation: 49

How to write a unicode character to a file that is not supported by utf-8, python

I'm trying to write some Hebrew text to a .txt file using python, but seeing as Hebrew is non-Ascii and non utf-8 I am getting errors. I am trying to have get literal text שלום in the text file as opposed to a representation of it.

hebrew_word = "שלום"

file = open("file_with_hebrew.txt", "w")
file.writelines(hebrew_word)
file.close()

part of my stack trace:

UnicodeEncodeError: 'charmap' codec can't encode character '\u05e9' in position 0: character maps to <undefined>

Upvotes: 1

Views: 267

Answers (2)

user1785721
user1785721

Reputation:

Your script work just fine. You are doing right and UTF-8 is OK to print those characters. What Python version are you using on what platform?

From open() doc:

In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding.

So you should specify the encoding when open the file to write in case that your platform doesn't have UTF-8 as default:

hebrew_word = "שלום"

with open("file_with_hebrew.txt", "w", encoding='UTF-8') as file
    file.writelines(hebrew_word)

Upvotes: 1

deceze
deceze

Reputation: 522510

hebrew_word = "שלום"

with open('file_with_hebrew.txt', 'w', encoding='utf-8') as file:
    #                                  ^^^^^^^^^^^^^^^^ 
    file.writelines(hebrew_word)

Make sure you specify an encoding when opening the file; in your case it defaulted to an encoding not able to represent Hebrew.

Upvotes: 2

Related Questions