Home > Back-end >  How to write a string of "Octal UTF-8 bytes" to a file in readable format in py 3.10? (n\
How to write a string of "Octal UTF-8 bytes" to a file in readable format in py 3.10? (n\

Time:07-09

I have a string object which has Octal UTF-8 bytes characters and I need to write them into a file in readable format. How to do that one in python 3.10?

Lets say I have group of German characters which are received in Octal UTF-8. What should I do here? I tried different encodings and decodings nothing is working out. Could some one please help me. I am new python learner and struggling to fix this out. Thanks in advance.

In simple words:

s = "n\303\244chsten"

What action I need to do to write it into a file as "nächsten" with open(myfile, 'w', encoding='utf-8') as fp: fp.write(s)

enter image description here

CodePudding user response:

Using latin1 instead of utf-8 works fine for me:

s = "n\303\244chsten"
with open('test.txt', 'w', encoding='latin1') as f:
    f.write(s)

CodePudding user response:

'latin-1' or 'iso-8859-1' encondings will work

  • Related