python3
s = r"b'\xe7\x86\x9f\xe8\xb7\xaf'"
s1 = "".join(s[2:-1].split(r"\x"))
s2 = bytes.fromhex(s1)
print(s2.decode(encoding="utf-8"))
#解出來為「熟路」
他那個檔案輸出的時候就有問題了...
怎麼會把bytes輸出成str
python2
from builtins import bytes
s = r"b'\xe7\x86\x9f\xe8\xb7\xaf'"
s1 = "".join(s[2:-1].split(r"\x"))
s2 = bytes.fromhex(s1)
print s2.decode("utf-8")
如果你是用py3的話,下面可以幫到你,py2就等高手吧.......
x = "b'\xe7\x86\x9f\xe8\xb7\xaf'"
x = x[2:-1]
print (x.encode('latin1').decode('utf8'))
x_s = "b'\\xe7\\x86\\x9f\\xe8\\xb7\\xaf'"
x_s = x_s[2:-1].encode('latin1').decode('unicode_escape')
print (x_s.encode('latin1').decode('utf8'))
result :
熟路
熟路