I’m getting a UnicodeEncodeError when I run the code below. It simply loops until i = 9000
, appends an html entity to a list based upon the value of i
, then writes the list to a file after looping. Doe’s anyone know where I’m going wrong?
JavaScript
x
5
1
for i in range(9000):
2
list.append(html.unescape("&#" + str(i) + ";"))
3
4
open("file.txt", "w").write(", ".join(list))
5
Advertisement
Answer
By default this open method doesn’t support unicode, so you have to set the correct encoding for that
JavaScript
1
2
1
open('file.txt', 'w', encoding='utf-8').write(", ".join(list))
2