【问题标题】:Python convert non standard charactersPython转换非标准字符
【发布时间】:2012-10-13 23:50:58
【问题描述】:

我有一个从包含一些非标准字符的网页中提取的列表。

列表示例:

[<td class="td-number-nowidth"> 10 115 </td>, <td class="td-number-nowidth"> 4 635 (46%) </td>, <td class="td-number-nowidth"> 5 276 (52%) </td>, ...]

带帽子的 A 应该是逗号。有人可以建议如何转换或替换这些,以便我可以在列表中的第一个值中获得值 10115?

源代码:

from urllib import urlopen
from bs4 import BeautifulSoup
import re, string
content = urlopen('http://www.worldoftanks.com/community/accounts/1000395103-FrankenTank').read()
soup = BeautifulSoup(content)

BattleStats = soup.find_all('td', 'td-number-nowidth')
print BattleStats

谢谢, 弗兰克

【问题讨论】:

  • 您之前的问题建议您使用应该自动处理字符编码的BeautifulSoup()。你如何获得Â? (提供一些代码)
  • 你说得对,J.F. 这是我正在使用的代码(上面发布)。

标签: python utf-8 replace ascii strip


【解决方案1】:

网站是否在其 Content-Encoding 标头中说明了编码?你必须得到它,并使用.decode 方法解码列表中的那些字符串。就像encoded_string.decode("encoding")。 encoding 可以是任何东西,utf-8 就是其中之一。

【讨论】:

  • 然后获取每个字符串并使用encoded_string.decode('utf-8')
  • 不幸的是,这个答案对我来说不起作用,因为网站 /said/ UTF-8,但都包含垃圾字符。
【解决方案2】:

您可以将.decode 方法与errors='ignore' 参数一起使用。

>>> s = '[ 10Â 115 , 4Â 635 (46%) , 5Â 276 (52%) , ...]'
>>> s.decode('ascii', errors='ignore')
u'[ 10 115 , 4 635 (46%) , 5 276 (52%) , ...]'

这里是help(''.decode):

decode(...)
    S.decode([encoding[,errors]]) -> object

    Decodes S using the codec registered for encoding. encoding defaults
    to the default encoding. errors may be given to set a different error
    handling scheme. Default is 'strict' meaning that encoding errors raise
    a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
    as well as any other name registered with codecs.register_error that is
    able to handle UnicodeDecodeErrors.

【讨论】:

  • 这对我有用,网站返回了声称编码之外的垃圾字符
【解决方案3】:

你有尝试吗?

这可能有效。

a =  ['10Â 115', '4Â 635 (46%)', '5Â 276 (52%)']
for b in a:
    b.replace("\xc3\x82 ", '')

输出:

10115
4635 (46%)
5276 (52%)

根据它的恒定性(如果它始终只是一个带点的 a),可能有更好的方法(用空白字符替换从 \ 到空格的任何内容)。

【讨论】:

    【解决方案4】:

    BeautifulSoup handles character encodings automatically。问题在于打印到您的控制台似乎不支持某些 Unicode 字符。在这种情况下是'NO-BREAK SPACE' (U+00A0)

    >>> L = soup.find_all('td', 'td-number-nowidth')
    >>> L[0]
    <td class="td-number-nowidth"> 10 123 </td>
    >>> L[0].get_text()
    u' 10\xa0123 '
    

    请注意,文本是 Unicode。检查print u'&lt;\u00a0&gt;' 是否适用于您的情况。

    在运行脚本之前,您可以通过更改 PYTHONIOENCODING 环境变量来控制所使用的输出编码。因此,您可以将输出重定向到指定utf-8 编码的文件,并使用ascii:backslashreplace 值在控制台中进行调试运行,而无需更改脚本。 bash 中的示例:

    $ python -c 'print u"<\u00a0>"' # use default encoding
    < >
    $ PYTHONIOENCODING=ascii:backslashreplace python -c 'print u"<\u00a0>"'
    <\xa0>
    $ PYTHONIOENCODING=utf-8 python -c 'print u"<\u00a0>"' > output.txt
    

    要打印相应的数字,您可以在不可破坏的空间上拆分以稍后处理项目:

    >>> [td.get_text().split(u'\u00a0')
    ...  for td in soup.find_all('td', 'td-number-nowidth')]
    [[u' 10', u'115 '], [u' 4', '635 (46%) '], [u' 5', u'276 (52%) ']]
    

    或者你可以用逗号替换它:

    >>> [td.get_text().replace(u'\u00a0', ', ').encode('ascii').strip()
    ...  for td in soup.find_all('td', 'td-number-nowidth')]
    ['10, 115', '4, 635 (46%)', '5, 276 (52%)']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2017-01-05
      • 2018-02-05
      • 1970-01-01
      • 2017-01-11
      • 2011-03-31
      相关资源
      最近更新 更多