NameError:name''未定义

时间:2017-05-09 15:06:43

标签: python variables merge

所以我正在合并两个文档并输出第三个文件

我收到错误

a = [[1,2,3],[4,5,6],[7,8,9]]
random.shuffle(a)

b = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
numpy.random.shuffle(b)

这就是我所做的:

代码:

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

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您似乎正在尝试写入文件 'document3&#39;并且您正试图从该文件中读取(根据您的评论)。如果是这种情况,您应首先读取该文件,然后您必须处理数据。所以代码将是

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.