I have an encoded string saved as a string in a file, it is possible to change that, if it works then. I wanna read it and get the real string back. Sorry I'm not good in explaining xD, here's my code:
def saveFile(src, con):
with open(src, "w") as f:
f.write(str(con))
f.close()
...
string = "юра"
saveFile("info", mlistsaver.encode())
this is the 'info' File:`
b'\xd1\x8e\xd1\x80\xd0\xb0'
but when I use this:
def get(src):
f = src
if path.isfile(f):
with open(f, "r") as f:
return f.read()
else:
return None
...
get("info").encode('iso-8859-1').decode('utf-8')
the string is just: b'\xd1\x8e\xd1\x80\xd0\xb0'
I know it has to do something with double \ but I couldn't fix it. As already said I can save the string in whatever format you want, I think the way I did it is really stupid.
Thank you guys!
>>> b'\xd1\x8e\xd1\x80\xd0\xb0'.decode('utf-8')
output:'юра'
- BpYstr()
on the value you want to write to a file. Ifcon
is abytes
object, you will have theb'...'
prefix+quotes literally in the output file, and also escape sequences with literal backslashes. Ifcon
is a byte string, open the file in binary mode (mode="wb"
) and directly write to it. - lenz