首页 > 解决方案 > 在python中“重新编译”时无法正确打印

问题描述

将“re.compile”插入行后,无法打印出汉字,谢谢!

比较表和行之间的输出:

表“title="添加置顶”的输出

从行输出“title="\u6dfb\u52a0\u7f6e\u9876"

#coding:utf-8
from bs4 import BeautifulSoup
import re

html = """"
<table align="center" bgcolor="#DDDDDD" border="0" cellpadding="0" cellspacing="0" class="mytable" id="table_live" width="100%"><tbody>
<tr align="center" id="tr_0" style="background-color:#6BADDF;color:white">
<td bgcolor="#ff9933" height="20" width="2%">选&lt;/td>
<td style="cursor:pointer;" title="角球总比数/半场比分" width="6%">角/半&lt;/td></tr>
<tr align="center" bgcolor="#F0F0F0" id="tr1_1545801" index="1" odds="" style="height:18px;display:none">
<td><input class="inp" id="chk_1545801" type="checkbox"/></td>
<td><a href="javascript:addConcern(1,13);" title="添加置顶"><img src="image/unTop.png"/></a></td></tr>
"""
soup = BeautifulSoup(html, "html.parser")
table = soup.find('table', id="table_live")
rows = table.findAll("tr", {"id" : re.compile('tr1_\d{7}')})

print table
print rows

1)table 可以打印汉字但是 2)rows 不能打印我们的汉字

表的输出

<table align="center" bgcolor="#DDDDDD" border="0" cellpadding="0" cellspacing="0" class="mytable" id="table_live" width="100%"><tbody><tr align="center" id="tr_0" style="background-color:#6BADDF;color:white">
<td bgcolor="#ff9933" height="20" width="2%">选&lt;/td>
<td style="cursor:pointer;" title="角球总比数/半场比分" width="6%">角/半&lt;/td></tr>
<tr align="center" bgcolor="#F0F0F0" id="tr1_1545801" index="1" odds="" style="height:18px;display:none"><td><input class="inp" id="chk_1545801" type="checkbox"/></td>
<td><a href="javascript:addConcern(1,13);" title="添加置顶"><img src="image/unTop.png"/></a></td></tr>
</tbody></table>

行的输出

[<tr align="center" bgcolor="#F0F0F0" id="tr1_1545801" index="1" odds="" style="height:18px;display:none"><td><input class="inp" id="chk_1545801" type="checkbox"/></td>\n<td><a href="javascript:addConcern(1,13);" title="\u6dfb\u52a0\u7f6e\u9876"><img src="image/unTop.png"/></a></td></tr>]

标签: pythonbeautifulsoup

解决方案


rows不会打印出中文字符,因为它是一个列表,并且默认打印列表的方式是显示它们包含的字符串的安全表示。例如,\u6dfb用于在我们可能无法显示 unicode 字符的情况下安全地表示字符添。我们可以通过在它前面加上 'u' 来强制这个字符串被解释为 unicode。

print '\u6dfb'
# \u6dfb
print u'\u6dfb'
# 添

无论如何,在您的示例中,如果我们打印单独的行,我们可以看到它们打印得很好,并且只是我们将 gthem 作为列表打印的事实导致了问题:

#coding:utf-8
from bs4 import BeautifulSoup
import re

html = """"
<table align="center" bgcolor="#DDDDDD" border="0" cellpadding="0" cellspacing="0" class="mytable" id="table_live" width="100%"><tbody>
<tr align="center" id="tr_0" style="background-color:#6BADDF;color:white">
<td bgcolor="#ff9933" height="20" width="2%">选&lt;/td>
<td style="cursor:pointer;" title="角球总比数/半场比分" width="6%">角/半&lt;/td></tr>
<tr align="center" bgcolor="#F0F0F0" id="tr1_1545801" index="1" odds="" style="height:18px;display:none">
<td><input class="inp" id="chk_1545801" type="checkbox"/></td>
<td><a href="javascript:addConcern(1,13);" title="添加置顶"><img src="image/unTop.png"/></a></td></tr>
"""
soup = BeautifulSoup(html, "html.parser")
table = soup.find('table', id="table_live")
rows = table.findAll("tr", {"id" : re.compile('tr1_*\d')})

print rows
# [<tr align="center" bgcolor="#F0F0F0" id="tr1_1545801" index="1" odds="" style="height:18px;display:none">\n<td><input class="inp" id="chk_1545801" type="checkbox"/></td>\n<td><a href="javascript:addConcern(1,13);" title="\u6dfb\u52a0\u7f6e\u9876"><img src="image/unTop.png"/></a></td></tr>]
for row in rows:
    print row
# <tr align="center" bgcolor="#F0F0F0" id="tr1_1545801" index="1" odds="" style="height:18px;display:none">
# <td><input class="inp" id="chk_1545801" type="checkbox"/></td>
# <td><a href="javascript:addConcern(1,13);" title="添加置顶"><img src="image/unTop.png"/></a></td></tr>

为了好玩,我们可以使用 repr 让 python 打印出你想要的内容(但请记住这里打印的类型是字符串)。

import sys
# Convert the list into a string representation that displays chinese characters that we can print...
representation = repr([x.encode(sys.stdout.encoding) for x in rows]).decode('string-escape')
print representation
# ['<tr align="center" bgcolor="#F0F0F0" id="tr1_1545801" index="1" odds="" style="height:18px;display:none">
# <td><input class="inp" id="chk_1545801" type="checkbox"/></td>
# <td><a href="javascript:addConcern(1,13);" title="添加置顶"><img src="image/unTop.png"/></a></td></tr>']
print(type(representation))
#<type 'str'>

为了获得更多乐趣,请尝试在 Python 3 中运行您的原始代码 - 您将看到您想要的确切输出,因为 Python 3 以更直观的方式处理 unicode。


推荐阅读