2011-09-09 58 views
0

我有一個數據文件(un-structed messy文件),我必須從中刪除特定的字符串列表(刪除字符串)。Python:從文件中刪除特定的字符串

下面是我在做什麼,但沒有結果:

infile = r"messy_data_file.txt" 
outfile = r"cleaned_file.txt" 

delete_list = ["firstname1 lastname1","firstname2 lastname2"....,"firstnamen lastnamen"] 
fin=open(infile,"") 
fout = open(outfile,"w+") 
for line in fin: 
    for word in delete_list: 
     line = line.replace(word, "") 
    fout.write(line) 
fin.close() 
fout.close() 

當我執行該文件,我得到以下錯誤:

NameError: name 'word' is not defined 

請幫幫忙!

+0

您是否收到任何類型的錯誤,或者它只是不輸出文件,但腳本似乎執行? – mwan

+0

不,我沒有收到任何錯誤..該文件似乎執行。 .py文件是否生成我可以查看的日誌文件。我沒有看到目錄中的任何內容。 – Zenvega

+0

執行後,clean_file.txt是否存在? – billinkc

回答

7

readlines方法返回的,而不是單詞的列表,讓你的代碼只會工作的地方你的話一個是通過自身的線。

由於files are iterators過線可以做到這一點很容易:

infile = "messy_data_file.txt" 
outfile = "cleaned_file.txt" 

delete_list = ["word_1", "word_2", "word_n"] 
fin = open(infile) 
fout = open(outfile, "w+") 
for line in fin: 
    for word in delete_list: 
     line = line.replace(word, "") 
    fout.write(line) 
fin.close() 
fout.close() 
+0

感謝羅斯..我試過你的代碼,除了我沒有看到optput文件。不知道爲什麼它沒有被創建。 – Zenvega

+0

上面的作品對我來說 –

+0

Appologies,我想刪除字符串..不知道如何使代碼工作。 – Zenvega

1

基於您的評論:「我雙擊.py文件似乎要調用的幾秒鐘後消失的Python應用程序。 。我沒有想到任何錯誤「我相信你的問題是腳本沒有找到輸入文件。這也是爲什麼你沒有得到任何輸出。當你雙擊它時......我實際上不記得解釋器在哪裏看,但我認爲它是安裝python.exe的地方。

使用像這樣的標準路徑。

# Depends on your OS 
infile = r"C:\tmp\messy_data_file.txt" 
outfile = r"C:\tmp\cleaned_file.txt" 

infile = r"/etc/tmp/messy_data_file.txt" 
outfile = r"/etc/tmp/cleaned_file.txt" 

另外,爲了您的理智,請從命令行運行它而不是雙擊。捕獲錯誤/輸出會容易得多。

+0

謝謝。我遵循你的建議。我看到一個爲空的clean_file.txt。當我在命令提示符下運行腳本時,我在fout.write(line)處得到一個錯誤。它說ValueError:關閉文件的I/O操作。不知道是什麼原因造成的。 – Zenvega

+2

問一個關於如何運行它的更新代碼和更新信息的新問題。 –

1

到同一個文件中刪除該字符串,我用這個代碼

f = open('./test.txt','r') 
a = ['word1','word2','word3'] 
lst = [] 
for line in f: 
    for word in a: 
     if word in line: 
      line = line.replace(word,'') 
    lst.append(line) 
f.close() 
f = open('./test.txt','w') 
for line in lst: 
    f.write(line) 
f.close() 
0

到OP, 羅斯帕特森的上述方法完全適用於我,即

infile = "messy_data_file.txt" 
outfile = "cleaned_file.txt" 

delete_list = ["word_1", "word_2", "word_n"] 
fin = open(infile) 
fout = open(outfile, "w+") 
for line in fin: 
    for word in delete_list: 
     line = line.replace(word, "") 
    fout.write(line) 
fin.close() 
fout.close() 

例子:

我有一個名爲messy_data_file.txt的文件,其中包含以下單詞(動物),不一定在同一行上。就像這樣:

Goat 
Elephant 
Horse Donkey Giraffe 
Lizard 
Bird 
Fish 

當我修改代碼來讀取(實際上只是增加的話,刪除了「delete_list」行):

infile = "messy_data_file.txt" 
outfile = "cleaned_file.txt" 

delete_list = ["Donkey", "Goat", "Fish"] 
fin = open(infile) 
fout = open(outfile, "w+") 
for line in fin: 
    for word in delete_list: 
     line = line.replace(word, "") 
    fout.write(line) 
fin.close() 
fout.close() 

由此產生的「cleaned_file.txt」看起來是這樣的:

Elephant 
Horse Giraffe 
Lizard 
Bird 

有一個空行,其中「喜羊羊」曾經是(其中,奇怪的是,除去「驢」沒有),但對於我而言,這工作正常。

我也加了輸入(「按回車鍵退出...」)代碼的最後部分,當我雙擊remove_text時,保持命令行窗口不會打開和關閉.py文件來運行它,但請注意,您不會以這種方式捕獲錯誤。

爲了做到這一點我在命令行中運行(其中C:\ Just_Testing是目錄,我的所有文件,即remove_text.py和messy_text.txt) 這樣的:

C:\Just_Testing\>py remove_text.py 

C:\Just_Testing>python remove_text.py 

的作品完全一樣。

當然,就像寫HTML的時候,我想這絕不會傷害到使用完全限定的路徑運行,從比你碰巧目錄之外的其他地方PY或Python時要坐在,如:

C:\Windows\System32\>python C:\Users\Me\Desktop\remove_text.py 

過程中的代碼,這將是:

infile = "C:\Users\Me\Desktop\messy_data_file.txt" 
outfile = "C:\Users\Me\Desktop\cleaned_file.txt" 

小心使用相同的完全合格的路徑,將您的新創建的cleaned_file.txt或將創建不論身在何處,並可能導致混亂的時候尋找它。

就我個人而言,我的環境變量中的PATH指向我所有的Python安裝,即C:\ Python3.5.3,C:\ Python2.7.13等,因此我可以從任何地方運行py或python。

無論如何,我希望對Patterson先生的這段代碼進行微調,可以準確地獲得您需要的內容。 :)

相關問題