我有一个子进程命令输出一些字符,如'\ xf1'.我正在尝试将其解码为utf8但我收到错误.
s = '\xf1'
s.decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
以上抛出:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf1 in position 0: unexpected end of data
Run Code Online (Sandbox Code Playgroud)
当我使用'latin-1'但是不应该使用utf8时它可以正常工作吗?我的理解是latin1是utf8的子集.
我在这里错过了什么吗?
编辑:
print s # ñ
repr(s) # returns "'\\xa9'"
Run Code Online (Sandbox Code Playgroud)