【问题标题】:Converting to plain text in python在python中转换为纯文本
【发布时间】:2020-12-24 04:59:01
【问题描述】:

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

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

【问题讨论】:

  • 对于 HTML 代码,您可以尝试 urllib.parse 中的一些功能。对于\xc3\xa,您可以将encode()decode() 一起尝试,但使用不同的值 - 请参阅Standard Encodings¶。可能您需要编码 raw_unicode_escapeunicode_escape 或两者。

标签: python string text utf-8


【解决方案1】:

对于\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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2012-04-12
    • 2014-09-08
    相关资源
    最近更新 更多