Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否有更好或更有效的方法来编写此代码? 重新导入 fr=open(“test.html”,“r”) i、 j,tablestart=0,0,0 str=“” p=重新编译((*?) 对于fr中的行: 如果“_Python_Regex_Html Table_Export To Csv - Fatal编程技术网

Python 是否有更好或更有效的方法来编写此代码? 重新导入 fr=open(“test.html”,“r”) i、 j,tablestart=0,0,0 str=“” p=重新编译((*?) 对于fr中的行: 如果“

Python 是否有更好或更有效的方法来编写此代码? 重新导入 fr=open(“test.html”,“r”) i、 j,tablestart=0,0,0 str=“” p=重新编译((*?) 对于fr中的行: 如果“,python,regex,html-table,export-to-csv,Python,Regex,Html Table,Export To Csv,可能是这样的: 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,tabl

可能是这样的:

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=""
fr中的行的
:
如果重新搜索(r'.+?“',第行):

line\u table=re.findall(r'\>\.+?\.+?\.+?\“我没有寻找任何html解析器。"-为什么?那会是一个更好的方法。我喜欢在使用别人的代码之前先编写我需要的东西。如果一行中有两行代码,我会有问题。有什么建议吗?你的代码假设html被新行分割,这并不总是正确的,整个表可以在一行中。我也会选择解析器。请查看对此问题投票最高的答案注意:关于正则表达式。如果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')