2014-09-25 66 views
0

所以我有一個文本文件,其中包含:清單文本或文本文件替換詞

Toy_One有序
Toy_Two有序
Toy_Three有序

我想從文字中的「有序」文件。我想製作一個清單,並重寫到文件將是簡單的,甚至是一個替代,但顯然不是。到目前爲止,我只能重寫Toy_Three。

Infile = (src.txt, "r") 
g = list(Infile) 
stripg = [mark.strip() for mark in g] 

for line in stripg: 
    entry = line.split(" Ordered") 
    NewList = list(entry[0]) 

    InOutFile = (src, "w") 
    for final in NewList: 
     InOutFile.write(final) 

我的結果就是Toy_Three

我也試過「泡菜」,但沒有成功。

任何幫助,將不勝感激,謝謝。

回答

0

沒關係,我明白了:

Infile = (src.txt, "r") 
magic = Infile.readlines() 

delete = [" Ordered"] 
Outfile = (src.txt, "w") 
for line in magic: 
    for word in delte: 
     line = line.replace(word,"") 
    Outfile.write(line)