2017-02-09 32 views
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) 
+0

我不認爲你的意思是'打印(結果)'...... – 2017-02-09 21:46:15

+0

呃,等等......'結果'是一個字符串.....你認爲'結果中的行'在做什麼?因爲我懷疑它在做你認爲正在做的事情。 – 2017-02-09 21:47:27

回答

0

with open (file, "w", ...

的 「W」 模式截斷文件(即每次你打開它時,文件被清除)。考慮「附加」的模式「a」。

+0

非常感謝,我對Python很陌生,因爲它一定很明顯...... :)你節省了我的一天。對此,我真的非常感激!! – Lee