我正在尝试从CSV文件构建python列表。我的CSV文件有5列,如下所示,用竖线(|)分隔:
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
以上是我的CSV测试用例。
我想根据文件的最后一列构建一个列表。
我想要一个列表,如果前两行在列[4]中有文本“sequence”,那么我应该在列表Seq1
中得到完整的行。
如果第3行,第4行和第5行有Parallel
,那么我应该在列表Par1
中获得完整的第3,第4和第5条记录。
然后,如果我在第6行和第7行有Sequence
,那么我应该在Seq2
中获取列表。
之后,如果我的文字为Parallel
,那么我应该将列表设为Par2
。
如何使用Python实现这一目标?
答案 0 :(得分:1)
我不知道为什么你需要分开seq1
和seq2
,或者会seq3
等等,但是这里有你如何查看你的文件:
import csv
seq1 = []
par1 = []
with open('test.csv', 'rb') as f:
r = csv.reader(f, delimiter='|', quotechar=' ')
for row in r:
if row[-1] == "Sequence":
seq1.append(row)
else:
par1.append(row)
print seq1
print par1
输出:
[['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']]
因此,如果您需要将它们分开,那么只需在附加到列表时添加if语句
也许不是像seq1,seq2等那样创建很多变量,你可以创建列表目录吗?例如:
import csv
d = {}
countSequence = 1
countParallel = 1
with open('kres.csv', 'rb') as f:
r = csv.reader(f, delimiter='|', quotechar=' ')
for 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"]
输出:
['QTP', 'Install', 'C:\\Cone_Automation\\RunTest.vbs', 'Install', 'Sequence']
因此,如果您需要第二个并行组,您只需将其称为:
print d["par2"]
答案 1 :(得分:0)
>>> import csv
>>> with open('eggs.csv', 'rb') as csvfile:
... spamreader = csv.reader(csvfile, delimiter='|', quotechar=' ')
... for row in spamreader:
... print ', '.join(row)
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
其中row
是包含该行上对象的列表。
您可以将行附加到另一个列表,为您提供所需的内容。
答案 2 :(得分:0)
Hi Thanks for your reply. Here how I did it.
import csv
class RecordDetails:
Token = ""
Records = []
csv_file = 'E:\\Automation_STAF\\AutomationDriver.csv'
testList = list(csv.reader(open(csv_file, 'r')))
curToken = "null"
recordDetails = RecordDetails()
recordDetails=None
records = [RecordDetails]
for index, line in enumerate(testList):
token="null"
token = testList[index][4]
if token=="null":
continue
if curToken !=token:
curToken =token
recordDetails = RecordDetails()
recordDetails.Token = curToken
recordDetails.Records=[]
records.append(recordDetails)
if recordDetails!=None:
recordDetails.Records.append(line)
#print(line)
#print(len(records))
#seq1=records[1].Records
#seq2=records[2].Records
#seq3=records[3].Records
#print(seq3)
d={}
CountSequence=1
#Listcount = len(records)
for i, obj in enumerate(records):
if records[i].Token=="Sequence":
d["seq" + str(CountSequence)] =records[i].Records
CountSequence +=1
if records[i].Token=="Parallel":
d["par" + str(CountSequence)] =records[i].Records
CountSequence +=1
print(len(records))
print (d["seq1"])
print (d["par2"])
print (d["seq3"])
Now here I am able to get all the directory of list. Thank you again for your help.