如何用变音符号替换 \xc3 等?

Aur*_*ler 4 python character-encoding python-3.x

spannkr \xc3\xa4ftig, da\xc3\x9f unser在 Python 中有一个输出。我如何用变音符号替换它?

sna*_*erb 5

德语字符已经存在,但编码为 utf-8。如果您想在解释器中看到变音等,那么您可以解码为str

>>> bs = b'spannkr \xc3\xa4ftig, da\xc3\x9f unser'
>>> s = bs.decode('utf-8')
>>> print(s)
spannkr äftig, daß unser
Run Code Online (Sandbox Code Playgroud)

您可能正在处理以str某种方式包含 utf-8 编码数据的 。在这种情况下,您需要执行一个额外的步骤:

>>> s = 'spannkr \xc3\xa4ftig, da\xc3\x9f unser'
>>> bs = s.encode('raw-unicode-escape')  # encode to bytes without double-encoding
>>> print(bs)
b'spannkr \xc3\xa4ftig, da\xc3\x9f unser' 
>>> decoded = bs.decode('utf-8')
>>> print(decoded)
spannkr äftig, daß unser
Run Code Online (Sandbox Code Playgroud)

没有一种简单的方法可以区分错误嵌入的空格和单词之间的空格。您需要使用某种拼写检查器或自然语言应用程序。