2017-05-09 115 views
-2

所以我合併兩個文件,並輸出第三文件NameError:名稱「」沒有定義

我得到的錯誤

Traceback (most recent call last): 
File "summarize.py", line 124, in <module> 
train_data = set(document3) 
NameError: name 'document3' is not defined 

這是我做了什麼:

代碼:

 filenames = ["/home/mustafa/data/combinedfile.txt", "/home/mustafa/data/sentences.txt"] 
    with open("document3", "wb") as outfile: 
     for fname in filenames: 
      with open(fname) as infile: 
        outfile.write(infile.read()) 
    train_data = set(document3) 

我在做什麼錯?

+3

很簡單,你沒有名爲'document3'的變量。 –

+0

林沒有pythonist,但它看起來像你忘記了一些「」在文檔3周圍 –

回答

1

看起來您正在嘗試寫入文件 'document3'並且您正試圖從該文件中讀取(根據您的評論)。如果是這種情況,您應該先閱讀該文件,然後再處理數據。所以代碼將是

filenames = ["/home/mustafa/data/combinedfile.txt", "/home/mustafa/data/sentences.txt"] 
with open("document3", "wb") as outfile: # here document3 is file name 
    for fname in filenames: 
     with open(fname) as infile: 
       outfile.write(infile.read()) 
train_data = set(open("document3").read().replace("\n","")) #this will read all data from document3 and stores as a set. 
+0

是的,我想同一個文件是我的train_data – Silas

+0

代碼根據評論改變.. – Mani

+0

如果我不想它作爲一組但只是一個文件? – Silas

相關問題