【问题标题】:Python create list from CSVPython从CSV创建列表
【发布时间】:2013-08-15 00:19:58
【问题描述】:

我正在尝试从 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] 中有文本“序列”,那么我应该在列表 Seq1 中获得完整的行。

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

然后,如果我在第 6 行和第 7 行有Sequence,那么我应该在Seq2 中获得列表。

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

如何使用 Python 实现这一点?

【问题讨论】:

  • 欢迎来到 Stackoverflow!到目前为止,您尝试过什么?

标签: python csv scripting


【解决方案1】:
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.

【讨论】:

    【解决方案2】:

    我不知道您为什么需要将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"]
    

    【讨论】:

    • 我需要 seq1,然后是 par1,然后是 seq2,然后是 par2,就好像它们出现在 csv 行中一样。这些是测试用例,如果它们是序列工作,我需要按顺序运行,如果它们是并行的有平行词。所以如果我的 50 个测试用例是并行的,那么我将在并行机器上执行它。所以我需要出现在 csv 中的列表。例如,如果前 10 个测试用例是连续的,那么它应该放在 seq1 中。那么如果接下来的 30 个案例在 parrlel 中,那么它应该在 par1 中。然后如果 5 在 seq 中,那么它应该在 seq2 中,同样。这就是我想要的方式再次感谢您的回复。
    • 是的。这取决于我们提供多少并行和顺序测试用例块。但是,如果我能够生成 2 个 par 和 2 个 seq 列表,那也会有所帮助。
    • 已更新答案,请检查是否适合您
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      相关资源
      最近更新 更多