相关疑难解决方法(0)

为什么这个转换为utf8不起作用?

我有一个子进程命令输出一些字符,如'\ 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)

python unicode encoding utf-8

14
推荐指数
1
解决办法
1万
查看次数

标签 统计

encoding ×1

python ×1

unicode ×1

utf-8 ×1