Python create list from CSV -
i trying build python list csv file. csv file has 5 columns shown below separated pipe(|):
qtp|install|c:\cone_automation\runtest.vbs|install|sequence qtp|open |c:\cone_automation\runtest.vbs|open |sequence qtp|install|c:\cone_automation\runtest.vbs|install|parallel qtp|install|c:\cone_automation\runtest.vbs|install|parallel qtp|install|c:\cone_automation\runtest.vbs|install|parallel qtp|open |c:\cone_automation\runtest.vbs|open |sequence qtp|install|c:\cone_automation\runtest.vbs|install|sequence qtp|open |c:\cone_automation\runtest.vbs|open |parallel qtp|open |c:\cone_automation\runtest.vbs|open |parallel qtp|open |c:\cone_automation\runtest.vbs|open |parallel
above test cases in csv.
i want build list based on last column of file. want list as, if first 2 lines having text "sequence" in column[4], should complete line in list seq1
.
if line 3rd, 4th , 5th having parallel
should complete 3rd, 4th , 5th records in list par1
.
then after that, if having sequence
in line 6th , 7th should list in seq2
.
after that, if having text parallel
should list par2
.
how can achieve using python?
i don't know why need separated seq1
, seq2
or there seq3
, on, here how can check file:
import csv seq1 = [] par1 = [] open('test.csv', 'rb') f: r = csv.reader(f, delimiter='|', quotechar=' ') row in r: if row[-1] == "sequence": seq1.append(row) else: par1.append(row) print seq1 print par1
output:
[['qtp', 'install', 'c:\\cone_automation\\runtest.vbs', 'install', 'sequence'], ['qtp', 'open ', 'c:\\cone_automation\\runtest.vbs', 'open ', 'sequence'], ['qtp', 'open ', 'c:\\cone_automation\\runtest.vbs', 'open ', 'sequence'], ['qtp', 'install', 'c:\\cone_automation\\runtest.vbs', 'install', 'sequence']] [['qtp', 'install', 'c:\\cone_automation\\runtest.vbs', 'install', 'parellel'], ['qtp', 'install', 'c:\\cone_automation\\runtest.vbs', 'install', 'parellel'], ['qtp', 'install', 'c:\\cone_automation\\runtest.vbs', 'install', 'parellel'], ['qtp', 'open ', 'c:\\cone_automation\\runtest.vbs', 'open ', 'parellel'], ['qtp', 'open ', 'c:\\cone_automation\\runtest.vbs', 'open ', 'parellel'], ['qtp', 'open ', 'c:\\cone_automation\\runtest.vbs', 'open ', 'parellel']]
so if need separate them, add if statements when appending list
maybe instead of creating lot of variable seq1, seq2 , etc. can create directory of list? example:
import csv d = {} countsequence = 1 countparallel = 1 open('kres.csv', 'rb') f: r = csv.reader(f, delimiter='|', quotechar=' ') row in r: if row[-1] == "sequence": d["seq" + str(countsequence)] = row countsequence += 1 else: d["par" + str(countparallel)] = row countparallel += 1 print d["seq1"]
output:
['qtp', 'install', 'c:\\cone_automation\\runtest.vbs', 'install', 'sequence']
so if need second parallel group, call this:
print d["par2"]
Comments
Post a Comment