【问题标题】:Python BeautifulSoup or CSV encoding issue with &nbsp&nbsp 的 Python BeautifulSoup 或 CSV 编码问题
【发布时间】:2013-11-10 12:52:47
【问题描述】:

我正在寻找将 HTML 表格转换为 CSV 格式的方法,结果发现了以下内容,看起来很有希望(因为我也在尝试学习 Python) https://stackoverflow.com/a/16697784/838253

很遗憾,它不适用于我的示例,并且我遇到了错误

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 753: ordinal not in range(128)

这似乎是BeautifulSoupstripped_strings将不间断空格 转换为u'\xa0'的结果 这看起来像完全正常的 Unicode(尽管将多个   转换为单个 `u'\xa0' 似乎有点不对)

错误似乎来自csv 模块。 为什么这不能处理标准 Unicode,最好的处理方法是什么?

【问题讨论】:

    标签: python csv unicode beautifulsoup


    【解决方案1】:

    在 Python 2.7 中,csv 模块不支持 unicode,请参阅documentation 开头的注释。

    您可以使用examples 中的UnicodeWriter 编写带有Unicode 的csv 数据。

    【讨论】:

      猜你喜欢
      • 2011-07-02
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      相关资源
      最近更新 更多