Python 3.x 脚本无法从“获取信息”;开放式;cmd读取文件
这是我的代码:Python 3.x 脚本无法从“获取信息”;开放式;cmd读取文件,python-3.x,readfile,Python 3.x,Readfile,这是我的代码: file1 = open('input_file1', 'r') # to read input_file1 file2 = open('input_file2', 'r') # to read input_file2 file3 = "output.txt" # the file include output writeOut = open(file3,'w') lines1=file1.readlines() # read all l
file1 = open('input_file1', 'r') # to read input_file1
file2 = open('input_file2', 'r') # to read input_file2
file3 = "output.txt" # the file include output
writeOut = open(file3,'w')
lines1=file1.readlines() # read all lines in file 1
lines2=file2.readlines() # read all lines in file 2
for line1 in lines1: # line1 is a list with each item representing a line of lines1
x = line1.split() # separate line into many column
for line2 in lines2: # line2 is a list with each item represent for a line of line2
y = line2.split() # separate line into many column
for x[1] in line1: # with any line of file 1
for y[2] in line2: # do with any line of file 2
if (x[1] in line1 == y[2] in line2): # if the word of column 2 of file 1 matches word of column 2 of file 2
writeOut.write(line1.lines1) # then write out it to the output file.
writeOut.close()
file1.close()
file2.close()
我不知道为什么我的output.txt是空的。
如果我的代码错了,请帮我指出错误。
谢谢大家! 大家好,欢迎来到StackOverflow!实际上,您没有修改
lines1
和lines2
的元素。您将获取每个元素,通过调用split
形成一个新事物,然后将该新事物分配给x
或y
,这并不重要,因为您在下一次迭代中覆盖了所述变量。实际上,我很惊讶对于x[1]在第1行:
甚至是有效的语法。我认为您已经有了一个良好的开端,而且实际上已经非常接近了。关于调试,我的建议是:(1)把东西打印到屏幕上,而不是打印文件,这样你就可以看到你得到了什么。(2) 打印出正在循环的任何内容的当前值。例如,如果你在l:…中对a说,那么在循环中再次打印a
,这样你就可以看到发生了什么。(3) 在if
语句之前,打印有关布尔条件的内容。例如,如果a==b和c==d:…
则在此之前,打印a、b、c和d的值,以及a==b和c==d的值。希望这有帮助。