NameError:名称“”没有定义
问题描述:
所以我合并两个文件,并输出第三文件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)
我在做什么错?
答
看起来您正在尝试写入文件 '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.
很简单,你没有名为'document3'的变量。 –
林没有pythonist,但它看起来像你忘记了一些“”在文档3周围 –