spen123
spen123

Reputation: 3524

Cant encode ascii character u'\xe9'

I am getting a message 'ascii' codec can't encode character u'\xe9' when I am writing a string to my file, heres how I am writing my file

my_file = open(output_path, "w")
my_file.write(output_string)
my_file.close()

I have been searching and found answers like this UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128) and the first one didn't work and then this one I'm confused why I am encoding data I want to be able to read

import io

f = io.open(filename, 'w', encoding='utf8')

Thanks for the help

Upvotes: 1

Views: 2300

Answers (1)

cdonts
cdonts

Reputation: 9599

As mentioned, you're trying to write non-ASCII characters with the ASCII encoding. Since the built-in open function doesn't support the encoding parameter, then consider always using io.open in Python 2.7 (which is the default since Python 3.x).

Upvotes: 0

Related Questions