![](/img/trans.png)
[英]which of these for-loops is more efficient/the better way to code the loop
[英]Is there a better or more efficient way to code this?
import re
fr=open("test.html",'r')
i,j,tablestart=0,0,0
str=""
p=re.compile("<td.*?>(.*?)<\/td>")
for line in fr:
if "<table" in line:
tablestart=1
elif "</table>" in line and tablestart==1:
j,tablestart=0,0
m=p.search(line)
if m and tablestart==1:
str+='"' + m.group(1) + '"' + ","
if "</tr>" in line and tablestart==1:
print(str)
str=""
该代码是从html表创建csv文件。 有没有更好或更有效的方式对此进行编码? 我没有在寻找任何HTML解析器。
也许是这样的:
for line in fr:
if re.search(r'"<td.*?>.+?<\/td>"',line):
line_table = re.findall(r'\>\.+?\<',line)
var = line_table
for var1 in var:
if var1 != False:
var2 = re.findall(r'\>\.+?\<',var1)[0]
output.write(var2+','+'\n')
else:
output.write(','+'\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.