encoding error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

encoding error

i have a encoding error wen i write in a file cause of a single unicode character '\x9' in a string : >>[...] >>var = "\x9" >>file.write(var+'\n') >>UnicodeError : can't encode ascii it's look like that I'm on pc

28th Oct 2016, 10:49 PM
AR2000
AR2000 - avatar
2 Answers
+ 2
Because bytes and characters are different entities. http://blog.notdot.net/2010/07/Getting-unicode-right-in-JUMP_LINK__&&__Python__&&__JUMP_LINK TL;DR: Don't use setdefaultencoding(); fix your failure to handle text correctly. In: "The correct solution is to fix your code so that you handle text correctly." ... - Use encode() on the byte string and specify the encoding...because it's going to the outside world. -Optionally prefix the string literal with u (unicode). Note: I couldn't immediately get Python 2.x or 3.x to accept var="\x9" so I didn't verify.
29th Oct 2016, 10:23 AM
Kirk Schafer
Kirk Schafer - avatar
0
i have a table variable how look like this f=["lol","l\x9l","mdr"] but a unicode string look like this "u' lel" so if i try encode() i will get the same error I've add my program here : Non working file writer
29th Oct 2016, 9:48 PM
AR2000
AR2000 - avatar