简体   繁体   中英

Python : Convert ascii string to unicode string

I have an ascii string, eg

"\\\\192.150.4.89\\tpa_test_python\\5.1\\\\videoquality\\rel_5.1.1Mx86\\blacklevelsetting\\\序\列\序\列.xml"

And I want to convert it into unicode and dump into a file, so that it gets dumped like:

"\\\\192.150.4.89\\tpa\\tpa_test_python\\5.1\\videoquality\\logs\\blacklevelsetting\\序列序列.xml"

Please share your thoughts.

Thanks, Abhishek

Use the unicode_escape codec. Python 3 example:

s=rb'\u005c\u005c192.150.4.89\u005ctpa_test_python\u005c5.1\u005cvideoquality\u005crel_5.1.1Mx86\u005cblacklevelsetting\u005c\u5e8f\u5217\u5e8f\u5217.xml'
s=s.decode('unicode_escape')
with open('out.txt','w',encoding='utf8') as f:
    f.write(s)

Output to file:

\\192.150.4.89\tpa_test_python\5.1\videoquality\rel_5.1.1Mx86\blacklevelsetting\序列序列.xml

Note: There was an extra backslash before videoquality that turned the v to a \\v character (vertical form feed) that I removed from your example string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM