Fix Python – Write to UTF-8 file in Python

I’m really confused with the codecs.open function. When I do:
file = codecs.open(“temp”, “w”, “utf-8”)
file.write(codecs.BOM_UTF8)
file.close()

It gives me the error

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position
0: ordinal not in range(128)

If I do:
file = open(“temp”, “w”)
file.write(codecs.BOM_UTF8)
file.close()

It w….

Fix Python – Writing Unicode text to a text file?

I’m pulling data out of a Google doc, processing it, and writing it to a file (that eventually I will paste into a Wordpress page).
It has some non-ASCII symbols. How can I convert these safely to symbols that can be used in HTML source?
Currently I’m converting everything to Unicode on the way in, joining it all together in a Python string, then….

Fix Python – “for line in…” results in UnicodeDecodeError: ‘utf-8’ codec can’t decode byte

Here is my code,
for line in open(‘u.item’):
# Read each line

Whenever I run this code it gives the following error:

UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xe9 in position 2892: invalid continuation byte

I tried to solve this and add an extra parameter in open(). The code looks like:
for line in open(‘u.item’, encoding=’utf-8′):
#….