My program is such that it takes in a string, and then tries to write that string to a file. I think that the issue is that the string has special characters (ü, ç, etc).
When I try to just write the string to a file, I get the compiler error (or something similar):
UnicodeEncodeError: 'charmap' codec cannot encode character '\u200b' in position 16: character maps to <undefined>
So then I wrote a function that looks like this:
def try_encode(info):
if info is None:
return None
temp = (str(info.encode('utf-8'))).replace("\n","")
return '"' + temp[2:len(temp)-1] + '"'
(I want to get rid of all newlines and write the string surrounded in quotes)
But the issue when I run this is that after the program runs, the file that I open has some strange characters in it that all start with: \x
Some examples of these characters are:
\xc3, \xa9p, \xaa, \xe2, \x80, etc
I think that these correspond to the special characters that I mentioned above. I have experimented with different encodings (utf-16, and utf-7), but they all either don't help or make these characters more common. Could anyone help me figure out how to get rid of these?
EDIT: including the code where I open the file:
f = open(filename, "w")