简体   繁体   English

如何使用python从csv文件中读取字符串下的特定列?

[英]How to read specific column under a string from csv file using python?

I was running a system performance test & I need to check for test results from a CSV file. 我正在运行系统性能测试,我需要检查CSV文件中的测试结果。 For example, my test.csv file shared below: 例如,我的test.csv文件共享如下:

https://docs.google.com/spreadsheets/d/1lUldk4N_5kEAETWdd2B5-x0UOpA_O0aus7x49fEqnPk/edit?usp=sharing https://docs.google.com/spreadsheets/d/1lUldk4N_5kEAETWdd2B5-x0UOpA_O0aus7x49fEqnPk/edit?usp=sharing

Please note that the results "PASS" or "FAIL" will be a hyperlink. 请注意,结果“PASS”或“FAIL”将是一个超链接。

Issue: I need to check if the driverproblem test is passed or not. 问题:我需要检查是否通过了driverproblem测试。 For example, if I run 100 cycles, the results under deviceproblem will be listed 100 times. 例如,如果我运行100个循环,则deviceproblem下的结果将列出100次。 If there is even 1 failure among the 100 cycles, the test is considered fail. 如果在100个循环中甚至有1个失败,则认为测试失败。

I am able to fetch the values(results) for one row under "driverproblem" field, but i am getting results in hyperlinks for all other fields that are present in the row instead of just one field. 我能够在“driverproblem”字段下获取一行的值(结果),但是我得到的结果是行中存在的所有其他字段的超链接而不是一个字段。

Is there any way, where i can just get the result "PASS" or "FAIL" for fields: "driverproblem" or "Devicemanager" or "Crash"? 有没有办法,我可以在字段中获得结果“PASS”或“FAIL”:“driverproblem”或“Devicemanager”或“Crash”?

I have tried with below: 我试过以下:

with open(filename, 'r') as csv_file:
for index,row in enumerate(csv_file):
        if "driverproblem" in row.split(','):
            for i in range(10):
                line = csv_file.next().split()
                a= line[2].split(',')
                print a

but i am getting results as below: 但我得到的结果如下:

['PASS:Basictest"")"', '"=HYPERLINK(""..\\..\\Reports\\testreport_03-20-19-07-37-01\\1.1.1_test_os\\Driver_info.xml""', '""PASS"")"', '"=HYPERLINK(""..\\..\\Reports\\testreport_03-20-19-07-37-01\\1.1.1_TEST_SYS\\drivertest_Log_Delta.xml""', '""PASS"")"', '"=HYPERLINK(""..\\..\\Reports\\testreport_03-20-19-07-37-01\\systemtestlogss\\systemtestlogs.csv""', '""PASS"")"', '"=HYPERLINK(""..\\..\\Reports\\testreport_03-20-19-07-37-01\\systemtestlogss\\systemtestlogs.csv""', '""PASS"")"', '"=HYPERLINK(""..\\..\\Reports\\testreport_03-20-19-07-37-01\\systemtestlogss\\systemtestlogs.csv""', '""PASS:Connected(via'] ['PASS:Basictest“”)“','”= HYPERLINK(“”.. \\ .. \\ Reports \\ testreport_03-20-19-07-37-01 \\ _ 1.1.1_test_os \\ Driver_info.xml“”',' “”PASS“”)“','”= HYPERLINK(“”.. \\ .. \\ Reports \\ testreport_03-20-19-07-37-01 \\ 1.1.1_TEST_SYS \\ drivertest_Log_Delta.xml“”','“”通过“”)“','”= HYPERLINK(“”.. \\ .. \\ Reports \\ testreport_03-20-19-07-37-01 \\ systemtestlogss \\ systemtestlogs.csv“”','“”PASS“”) “','”= HYPERLINK(“”.. \\ .. \\ Reports \\ testreport_03-20-19-07-37-01 \\ systemtestlogss \\ systemtestlogs.csv“”','“”PASS“”)“',' “= HYPERLINK(”“.. \\ .. \\ Reports \\ testreport_03-20-19-07-37-01 \\ systemtestlogss \\ systemtestlogs.csv”“','”“PASS:已连接(通过']

whereas i need only the field ""=HYPERLINK(""..\\\\..\\\\Reports\\\\testreport_03-20-19-07-37-01\\\\1.1.1_TEST_SYS\\\\drivertest_Log_Delta.xml""', '""PASS"")" which is under "Drivertest" field in csv file. 而我只需要字段""=HYPERLINK(""..\\\\..\\\\Reports\\\\testreport_03-20-19-07-37-01\\\\1.1.1_TEST_SYS\\\\drivertest_Log_Delta.xml""', '""PASS"")"在csv文件中的”Drivertest“字段下。

Try this: 尝试这个:

with open("test.csv", 'r') as csv_file:
        arr = []
        crash = []
        devicemanager = []
        for row in csv_file:                              
                rowarr = row.split(',')
                arr.append(rowarr)
                devicemanager.append(rowarr[5])
                crash.append(rowarr[7])

Output: 输出:

 ['\\n', '\\n', '\\n', '\\n', '\\n', '\\n', 'Crash\\n', 'PASS\\n', 'NA'] ['', '', '', '', '', '', 'Devicemanager', 'PASS', 'NA'] 

You can iterate through the lists crash and device manager to find the fields PASS and FAIL. 您可以遍历列表崩溃和设备管理器以查找字段PASS和FAIL。 Note that this is columns 5 and 7 of test.csv. 请注意,这是test.csv的第5列和第7列。

Total No Of Cycles,2,,,,,,
Passed Cycles,2,,,,,,
Failed Cycles,0,,,,,,
Ignored Cycles,0,,,,,,
,,,,,,,
,,,,,,,
Cycle Number,Cycle PASS/FAIL,Expected State,Test,Driver problem,Devicemanager,Memory Dump,Crash
1,PASS,S4,PASS:S4,PASS,PASS,PASS,PASS
2,PASS,S4,PASS:S4,NA,NA,NA,NA

DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. 弃用:Python 2.7将于2020年1月1日到期。请升级您的Python,因为在该日期之后将不再维护Python 2.7。 source: repl.it 来源:repl.it

For a cleaner version, use the csv module--it's quicker to write code with it: 对于更干净的版本,请使用csv模块 - 使用它编写代码会更快:

import csv

csv_file_path = 'test.csv'
csvFile = open(csv_file_path,'r')
csvrdr = csv.reader(csvFile, delimiter=',')
devicemanager = []
crash = []
for row in csvrdr:
    devicemanager.append(row[5])
    crash.append(row[7])


print(crash)
print(devicemanager)

Output: 输出:

 ['', '', '', '', '', '', 'Crash', 'PASS', 'NA'] ['', '', '', '', '', '', 'Devicemanager', 'PASS', 'NA'] 

Both of these examples will work on python 2 and 3, so you should consider upgrading to python 3, as python 2 is being deprecated. 这两个示例都适用于python 2和3,因此您应该考虑升级到python 3,因为python 2已被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM