1
我無法將從多個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 21:46:15
呃,等等......'結果'是一個字符串.....你認爲'結果中的行'在做什麼?因爲我懷疑它在做你認爲正在做的事情。 – 2017-02-09 21:47:27