Aug-06-2018, 03:49 PM
(This post was last modified: Aug-06-2018, 03:49 PM by Qwert_Yuiop.)
I am trying to write characters to a file.
My problem is that for for N=128,
I think it is very weird that something that is allowed in
The whole code for this example is:
My python version is Anaconda 3.6.5 and running on windows 7.
My problem is that for for N=128,
file.write(chr(N))
fails and returns error: UnicodeEncodeError: 'charmap' codec can't encode character '\x80'.
I think it is very weird that something that is allowed in
print()
would return an error in file.write()
. However for N=255 it does not return an error, so it is not simply that it fails for all N>127.The whole code for this example is:
N=128 print(chr(N)) #This works just fine. file=open('output.txt','w') file.write(chr(N)) #This one returns errors! file.close()I also tried utf-8, but with no luck.
N=128 print(chr(N)) #This works just fine. file=open('output.txt','w',encoding="utf-8") file.write(chr(N)) #No error but output.txt is empty! file.close()It gives no errors, but the output file is empty.
My python version is Anaconda 3.6.5 and running on windows 7.