-1

我正在尝试从 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 中的测试用例。

我想根据文件的最后一列构建一个列表。我想要一个列表,如果前两行在 column[4] 中有文本“sequence”,那么我应该在 list 中获得完整的行Seq1

如果第 3、第 4 和第 5 行有,Parallel那么我应该在 list 中获得完整的第 3、第 4 和第 5 条记录Par1

然后在那之后,如果我Sequence 在第 6 行和第 7 行,那么我应该在Seq2.

之后,如果我有文本,Parallel那么我应该将列表作为Par2.

如何使用 Python 实现这一目标?

4

3 回答 3

1

我不知道您为什么需要分开seq1seq2或者会分开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"]
于 2013-08-13T11:54:01.667 回答
0

谷歌 - Python CVS

>>> 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

Whererow是包含该行上的对象的列表。您可以将行附加到另一个列表,给出您想要的内容。

于 2013-08-13T11:45:36.177 回答
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.
于 2013-08-14T08:29:47.163 回答