2017-05-09 96 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

相关问题