2013-10-23 107 views
0

我有一個ascii字符串,例如Python:將ascii字符串轉換爲unicode字符串

「\ u005c \ u005c192.150.4.89 \ u005ctpa_test_python \ u005c5.1 \ u005c \視頻質量\ u005crel_5.1.1Mx86 \ u005cblacklevelsetting \ u005c \ u5e8f \ u5217 \ u5e8f \ u5217.xml」

而且我想將其轉換成Unicode和轉儲到一個文件,這樣它就會像傾倒:

「\\ 192.150.4.89 \噸\ tpa_test_python \ 5.1 \視頻質量\日誌\ blacklevelsetting \序列序列的.xml」

請分享你的想法。

感謝, 阿布舍克

回答

0

使用unicode_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字符(垂直進紙),我從你的示例中刪除串。