如何转换python中特殊字符的编码?

Yon*_*tai 0 python character-encoding

我有一个包含一些句子的文件。但其中一些包含一些有线字符(\xe2\x88\x9a\xe2\x80\xa2、\xe2\x88\x9a\xc2\xa7、\xe2\x88\x9a\xc3\x91),如下所示。它们是什么?有没有办法将它们转换回 python 中的普通字符?

\n\n

谢谢,

\n\n

例子。

\n\n

Is there an outdoor grill/bbq place? P\xe2\x88\x9a\xc2\xa7r

\n\n

Hej Hur l\xe2\x88\x9a\xe2\x80\xa2ngt aa\xe2\x88\x9a\xc2\xa7r de till Stallarna? MVH LAILA

\n\n

\xe2\x88\x9a\xc3\x91r d\xe2\x88\x9a\xc2\xa7r sandstrand och hur l\xe2\x88\x9a\xe2\x80\xa2ngt

\n

fur*_*ras 6

看起来它使用了错误的编码 - MacRoman- 而不是UTF-8. 可能是MacOS系统。

\n\n

如果我使用它对其进行编码(到字节)MacRoman,然后使用将其解码回字符串,utf-8那么我会得到正确的文本

\n\n
text = \'\'\'Is there an outdoor grill/bbq place? P\xe2\x88\x9a\xc2\xa7r\n\nHej Hur l\xe2\x88\x9a\xe2\x80\xa2ngt aa\xe2\x88\x9a\xc2\xa7r de till Stallarna? MVH LAILA\n\n\xe2\x88\x9a\xc3\x91r d\xe2\x88\x9a\xc2\xa7r sandstrand och hur l\xe2\x88\x9a\xe2\x80\xa2ngt\'\'\'\n\ntext = text.encode(\'MacRoman\').decode(\'utf-8\') \nprint(text)\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果:

\n\n
Is there an outdoor grill/bbq place? P\xc3\xa4r\n\nHej Hur l\xc3\xa5ngt aa\xc3\xa4r de till Stallarna? MVH LAILA\n\n\xc3\x84r d\xc3\xa4r sandstrand och hur l\xc3\xa5ngt\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

在 Linux Mint 19.2、Python 3.7 上测试

\n\n

MacRoman有关问题的信息如何解码这些字符?\xe2\x88\x9a\xc2\xb0 \xe2\x88\x9a\xc2\xa9 \xe2\x88\x9a\xe2\x89\xa0

\n