html提交多个正则表达式,将多个html文件的正则表达式结果写入.txt outfile
我無法將從多個html文件(文本不是英文)獲得的正則表達式結果寫入.txt文件。它將它們打印成屏幕上新行的幾個字符串,但是當我嘗試將它寫入outfile時,它只會寫入一個隨機字符串。我的代碼看起來像這樣: 你能幫我怎么把所有的字符串寫入所有大約100個文件的outfile中嗎?將多個html文件的正則表達式結果寫入.txt outfile
from bs4 import BeautifulSoup
import sys
import string
import re
import os
text = glob.glob('C:/Users/dell/Desktop/python-for-text-analysis-master/Notebooks/MEK/*')
for filename in text:
with open(filename, encoding='ISO-8859-1', errors="ignore") as f:
mytext = f.read()
soup = BeautifulSoup(mytext, "lxml")
extracted_text = soup.getText()
pattern = r"\ba\b\s\bleg[\w]+bb\b\s\b[\w]+\b"
result = (", ".join(re.findall(pattern, mytext)))
file = "C:/Users/dell/Desktop/python-for-text-analysis-master/Data/Charlie/charlie_neww.txt"
for row in result:
with open (file, "w", encoding="iso-8859-1", errors="ignore") as outfile:
print(result, end='\n', file=outfile)
2017-02-09
Lee
+0
我不認為你的意思是'打印(結果)'...... –
2017-02-09 21:46:15
+0
呃,等等......'結果'是一個字符串.....你認為'結果中的行'在做什么?因為我懷疑它在做你認為正在做的事情。 –
2017-02-09 21:47:27
總結
以上是生活随笔為你收集整理的html提交多个正则表达式,将多个html文件的正则表达式结果写入.txt outfile的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 存定期一年哪个银行高 定期存款一年的利息
- 下一篇: html5内容切换特效,html5+jQ