循环两个文件只打印第一行 python
loop for two files only prints first line python
- 第一个文件
f1
有两列,第一列是 ID number
,第二列是 value associated
。
- 第二个文件
f2
是第一个文件的更大版本,具有更多值 six columns
但包含第一个文件中的两个值。
- 第二个文件有一个列,我想将其与第一个文件中的值相关联,我希望输出为一个新的
text file
,其中包含 ID
、
- 关联的值和另一列与我想从更大的第二个文件关联的值。
- 到目前为止,我已经编写了一个代码,它正在执行我想要的操作,但它只打印出第一行。
我并不擅长 python 这在我的代码中可能很明显,我希望有人能回答我的问题
import csv
with open('output1.txt','w') as out1,open('list1.csv') as f1,open('list2.csv') as f2:
csvf1=csv.reader(f1)
csvf2=csv.reader(f2)
for txt1 in csvf1:
id1=txt1[0]
z1=txt1[1]
for txt2 in csvf2:
id2=txt2[0]
z2=txt2[3]
ra=txt2[1]
if id1==id2:
out1.write("{} {} {}\n".format(id2,z1,ra))
out1.close()
f1.close()
f2.close()
我还想指出,出于某种原因,使用 .split(',')
对我的文件不起作用,以防万一有人试图在答案中使用它。
将 csvf2=csv.reader(f2) 行保留在第一个循环内。内部循环只会在第一行执行。对于第二行,内部循环不会被执行,因为文件读取器标记已经在文件末尾。
import csv
with open('output1.txt','w') as out1,open('list1.csv') as f1,open('list2.csv') as f2:
csvf1=csv.reader(f1)
for txt1 in csvf1:
id1=txt1[0]
z1=txt1[1]
csvf2=csv.reader(f2)
for txt2 in csvf2:
id2=txt2[0]
z2=txt2[3]
ra=txt2[1]
if id1==id2:
out1.write("{} {} {}\n".format(id2,z1,ra))
out1.close()
f1.close()
f2.close()
cvs.reader()
是一个函数,您只能迭代它的结果 一次,(它是 yield
函数吗?有人应该更正我,我只是深入研究了源代码并卡在了对象 reader()
的 buildin-module。无论如何,它的行为就像一个 yield 函数)
因此您可能需要将每一行保存在临时数组中以供进一步使用:
list1 = []
with open('list1.txt') as fp:
for row in csv.reader(fp):
list1.append(row)
顺便说一句,当您使用 with
表达式打开 fp
时,您不需要显式关闭它,该机制会在您离开 with
时自动为您关闭]范围。
我设法从另一个程序员那里找到了我的答案,这是最终有效的代码。
非常感谢您的回答,因为它们接近有效。
import csv
with open('output1.txt','w') as out1, open('file1.csv') as f1:
csvf1=csv.reader(f1)
for txt1 in csvf1:
id1=txt1[0]
z1=txt1[1]
with open('file2.csv') as f2:
csvf2=csv.reader(f2)
for txt2 in csvf2:
id2=txt2[0]
z2=txt2[3]
ra=txt2[1]
if id1 == id2:
out1.write("{} {} {}\n".format(id2,z1,ra))
- 第一个文件
f1
有两列,第一列是ID number
,第二列是value associated
。 - 第二个文件
f2
是第一个文件的更大版本,具有更多值six columns
但包含第一个文件中的两个值。 - 第二个文件有一个列,我想将其与第一个文件中的值相关联,我希望输出为一个新的
text file
,其中包含ID
、 - 关联的值和另一列与我想从更大的第二个文件关联的值。
- 到目前为止,我已经编写了一个代码,它正在执行我想要的操作,但它只打印出第一行。
我并不擅长 python 这在我的代码中可能很明显,我希望有人能回答我的问题
import csv with open('output1.txt','w') as out1,open('list1.csv') as f1,open('list2.csv') as f2: csvf1=csv.reader(f1) csvf2=csv.reader(f2) for txt1 in csvf1: id1=txt1[0] z1=txt1[1] for txt2 in csvf2: id2=txt2[0] z2=txt2[3] ra=txt2[1] if id1==id2: out1.write("{} {} {}\n".format(id2,z1,ra)) out1.close() f1.close() f2.close()
我还想指出,出于某种原因,使用
.split(',')
对我的文件不起作用,以防万一有人试图在答案中使用它。
将 csvf2=csv.reader(f2) 行保留在第一个循环内。内部循环只会在第一行执行。对于第二行,内部循环不会被执行,因为文件读取器标记已经在文件末尾。
import csv
with open('output1.txt','w') as out1,open('list1.csv') as f1,open('list2.csv') as f2:
csvf1=csv.reader(f1)
for txt1 in csvf1:
id1=txt1[0]
z1=txt1[1]
csvf2=csv.reader(f2)
for txt2 in csvf2:
id2=txt2[0]
z2=txt2[3]
ra=txt2[1]
if id1==id2:
out1.write("{} {} {}\n".format(id2,z1,ra))
out1.close()
f1.close()
f2.close()
cvs.reader()
是一个函数,您只能迭代它的结果 一次,(它是 yield
函数吗?有人应该更正我,我只是深入研究了源代码并卡在了对象 reader()
的 buildin-module。无论如何,它的行为就像一个 yield 函数)
因此您可能需要将每一行保存在临时数组中以供进一步使用:
list1 = []
with open('list1.txt') as fp:
for row in csv.reader(fp):
list1.append(row)
顺便说一句,当您使用 with
表达式打开 fp
时,您不需要显式关闭它,该机制会在您离开 with
时自动为您关闭]范围。
我设法从另一个程序员那里找到了我的答案,这是最终有效的代码。
非常感谢您的回答,因为它们接近有效。
import csv
with open('output1.txt','w') as out1, open('file1.csv') as f1:
csvf1=csv.reader(f1)
for txt1 in csvf1:
id1=txt1[0]
z1=txt1[1]
with open('file2.csv') as f2:
csvf2=csv.reader(f2)
for txt2 in csvf2:
id2=txt2[0]
z2=txt2[3]
ra=txt2[1]
if id1 == id2:
out1.write("{} {} {}\n".format(id2,z1,ra))