Python:将ascii字符串转换为unicode字符串
我有一个ascii字符串,例如 “\u005c\u005c192.150.4.89\u005ctpa\u test\u python\u005c5.1\u005c\videoquality\u005crel\u 5.1.1Mx86\u005cblacklevelsetting\u005c\u5e8f\u5217\u5e8f\u5217.xml” 我想把它转换成unicode并转储到一个文件中,这样就可以像这样转储: “\\192.150.4.89\tpa\tpa\u test\u python\5.1\videoquality\logs\blacklevelsetting\序列序列.xml“ 请分享你的想法 谢谢,Python:将ascii字符串转换为unicode字符串,unicode,python-unicode,asciiencoding,Unicode,Python Unicode,Asciiencoding,我有一个ascii字符串,例如 “\u005c\u005c192.150.4.89\u005ctpa\u test\u python\u005c5.1\u005c\videoquality\u005crel\u 5.1.1Mx86\u005cblacklevelsetting\u005c\u5e8f\u5217\u5e8f\u5217.xml” 我想把它转换成unicode并转储到一个文件中,这样就可以像这样转储: “\\192.150.4.89\tpa\tpa\u test\u python\
阿披舍克使用
unicode\u escape
编解码器。Python 3示例:
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)
输出到文件:
\\192.150.4.89\tpa_test_python\5.1\videoquality\rel_5.1.1Mx86\blacklevelsetting\序列序列.xml
注意:在videoquality
之前有一个额外的反斜杠,它将v
变成了一个\v
字符(垂直表单提要),我从示例字符串中删除了该字符