我有一个csv文件,其中包含如下行:
113,25CNFW,"test1",1ALNAU,25CNFW
113,25D5L2,"test2",1ALNAU,25D5L2
114,MCF82H,"test3",307531,MCF82H
现在,我试图通过搜索第一列中的某些编号来提取每行的第二列和第四列,但是每当第一列包含重复项时,我的脚本逻辑都会失败,因为它将重复项视为单个条目。有什么方法可以遍历python中的重复项,以使每个重复值都被视为单个值?
import csv
mycsv = "/home/test.csv"
def csv_read(fobj, mid):
reader = csv.reader(fobj)
for row in reader:
if mid in row:
contid = row[1]
accid = row[3]
return accid,contid
list = ['113','114']
for mid in list:
with open(mycsv,"r") as fobj:
detail = csv_read(fobj,mid)
print detail[0] + "," + detail[1]
输出:
1ALNAU,25CNFW
307531,MCF82H
Process finished with exit code 0
所以,简而言之,我希望113被考虑两次。
我尝试过使用其他选项,但是它们不起作用:(