python - 在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%">选</td>
<td style="cursor:pointer;" title="角球总比数/半场比分" width="6%">角/半</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%">选</td>
<td style="cursor:pointer;" title="角球总比数/半场比分" width="6%">角/半</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>]
解决方案
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%">选</td>
<td style="cursor:pointer;" title="角球总比数/半场比分" width="6%">角/半</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。
推荐阅读
- java - 类中未知类型的数组实例化
- arrays - malloc/realloc 只适用于无用的 printf
- php - 声明与接口不兼容(PHP)
- c# - ImageCropper.Forms 在初始化时失败
- c# - PlatformNotSupportedException:此平台不支持安全二进制序列化
- swift - SwiftUI 生命周期不支持 CoreData
- android - 在 HMS 设备上使用 Geocoder 对纬度/经度坐标进行反向地理编码
- java - 如何在 android Studio 的编辑文本中使用数学符号?
- regex - 将键/值对与注释匹配
- powerbi - DAX 计算列,如果每天和每个产品 ID 位于前 N 个中,则设置一个标志