2016-04-19 15 views
0
tran = open("rep-small.txt") 
stop = open("stopSQL.txt") 
cTran = open("cleanTran.txt.","w") 

badList = [] 
for line in stop: 
    badList.append(line) 

def cleantxt(): 
    for line in tran: 
     for word in badList: 
      line = line.replace(word,"") 
     cTran.write(line) 

cTran.txtを書いていますが、停止から単語を取り出していません。 停止ファイルは、このようにフォーマットされます。あるテキストファイルの単語を別のテキストファイルに取り込むにはどうすればよいですか? Python

異なるライン上の各単語と

それ

+0

これら 質問には多くの間違いがある。あなたのtran.txtに最初にあるのは – Taylan

+0

です。これは議論の写しです。私は最終的にそれに関連する言葉を数えたいと思いますが、今は私には関係のない言葉をa、the、it、andのように取り出す必要があります。 –

答えて

-1

あなたはあなただけのforループをスキップすることができます

for line in stop.readlines(): 
0

を試し、線として "停止" 読んだことがない:(ところであなたは)(関数cleantxtを呼び出している?)

tran = open("rep-small.txt") 

cTran = open("cleanTran.txt.","w") 

def cleantxt(): 
    for line in tran.readlines(): 
     for word in badList: 
      line = line.replace(word,"") 
     cTran.write(line) 
    ctran.close() 

with open("stopSQL.txt") as stop: # The With-Statement calls the close() function after its finished executing 
    badList = stop.readlines() # Add lines to list 

cleantxt() 
+0

はい、私の関数はcleantxt()です、私は後で別の関数内でそれを使用します。 –

+0

私はそれをシェルで呼びますが、何もしません。 –

+0

私は編集を行います。今すぐお試しください – Oisin

関連する問題