I was having some trouble with using unicode in python, so I wrote this program, and I am confused by the results. Whenever I run it, different characters give me error #2, which means that utf32, utf16 and utf8 all gave errors when I tried to write a unicode character to my test file. Never the same ones. Is it a problem with my program, or am I doing somthing python is not designed to handle?
for a in range(65535):
try:
open('test_text.txt','w').write(unichr(a).encode("utf32"))
if len(open('test_text.txt','r').read()) == 0:
print unichr(a) + ' Error #1 #' + str(a)
except IOError:
try:
open('test_text.txt','w').write(unichr(a).encode("utf16"))
except IOError:
try:
open('test_text.txt','w').write(unichr(a).encode("utf8"))
except IOError:
print unichr(a) + ' Error #2 #' + str(a)
except UnicodeEncodeError:
print unichr(a) + ' Error #3 #' + str(a)
raw_input('\n\nEnter char to end:')