【发布时间】:2019-05-05 18:28:19
【问题描述】:
我正在尝试在两个标签 <example>text</example> 之间提取文本。我找到了一个可以使用正则表达式来做到这一点的帖子;但是,当我尝试在 Python 中使用它时,我不得不转义字符。
original regex : run = re.findall("(?<=(<runs>))(\w|\d|\n|[().,\-:;@#$%^&*\[\]"'+–/\/®°⁰!?{}|`~]| )+?(?=(</runs>))", text)
完整代码:
#text is a text file but there is too much data to process to post it here
text = "<os>Windows Vista or Windows 7</os><filename>AS_ENGINE.EXE</filename><header_size>240</header_size><atime>2019-04-28T13:34:34Z</atime><runs>1</runs><filenames><file>
<os>Windows Vista or Windows 7</os><filename>CHRMSTP.EXE</filename><header_size>240</header_size><atime>2019-04-28T13:15:32Z</atime><runs>2</runs><filenames>
<os>Windows Vista or Windows 7</os><filename>RUNDLL32.EXE</filename><header_size>240</header_size><atime>2019-04-28T13:07:35Z</atime><runs>1</runs><filenames><file>"
soup = BeautifulSoup(text, "lxml")
for x in soup.find_all("runs"):
print("Orginal ", x)
for x in soup.find_all("dir"):
print("Orginal ", x)
for x in soup.find_all("filename"):
print("Orginal ", x)
然后我想将某些标签写入 csv...
fieldnames = 'File Nmae','Number of runs','File Path'
with open("C:\\ProgramData\\processed\\winprefetch.csv", 'w', newline='', encoding="utf8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(fieldnames)
writer.writerows([[diskimage_name * row], filename, numberofruns,file]
【问题讨论】: