【问题标题】:encoding slovak letters like: á š Č é编码斯洛伐克字母,如:ášČé
【发布时间】:2015-12-21 12:07:29
【问题描述】:

从我搜索的一个网页中得到一个词“Zápas”。问题是我的数据中最接近的版本看起来像:windows-1250 编码后的 Z\xe1pas。 没有那个 str(a) 会有一些 ANSI 错误。 谢谢您的帮助。

我的尝试:

def encode(text):
    return text.encode('windows-1250')...
...
for cell in row.findAll(['td', 'th']):
    cell=encode(cell.get_text().strip())
    a.append(cell)
foo.write(str(a)+"\n")

【问题讨论】:

  • 你为什么要手动编码而不是让foo处理它?
  • 对于任何涉及 unicode 的内容,指定相关的 Python 2 或 Python 3 以及(最好)标记非常重要。

标签: python encoding diacritics


【解决方案1】:

a 字符串可能实际上是一个 unicode 字符串。要将其写入字节流,您应该将其编码为指定编码的字节字符串,在您的情况下为windows-1250,在我的情况下为utf-8。只需使用a.encode(<encoding>) before sending result to thefoo.write()`:

foo.write(a.encode("utf-8"))

我还会在将数据“外部”发送(到文件、到远程目标等)之前立即处理 unicode 中的整个数据并转换为字节字符串

【讨论】:

    猜你喜欢
    • 2014-05-22
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2022-01-23
    • 2021-12-03
    相关资源
    最近更新 更多