在 python 中转换为纯文本

Converting to plain text in python

我有一个数据框列 ("albums"),其中大多数值都以纯文本编码(例如:"Album""Album 2" 等),但有些值有 utf-8 或其他值与纯文本相结合。例如,代替文本 "Précis" 我有 "Pr\xc3\xa9cis." 似乎还有一些 HTML 编码,例如 "\'" 代替文本中的撇号。

是否有一种简单的方法可以将所有内容转换为纯文本,而无需搜索和替换每个可能的内容utf/unicode/html?

对于 \xc3\xa9 你需要组合 encode()decode()raw_unicode_escape

print( "Pr\xc3\xa9cis.".encode('raw_unicode_escape').decode() )

文档:编解码器 Python Specific Encodings


' 你需要 html.unescape

import html

print(html.unescape("'"))

文档:html