Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CSV解析Python,输出具有特定值的特定行_Python_Python 2.7_Csv_Parsing - Fatal编程技术网

CSV解析Python,输出具有特定值的特定行

CSV解析Python,输出具有特定值的特定行,python,python-2.7,csv,parsing,Python,Python 2.7,Csv,Parsing,我想使用python解析CSV文件,并只输出具有特定值的特定行。这是我到现在为止的代码 import csv f = open('alerts2.csv') csv_f = csv.reader(f) li1 = [] header = next(csv_f) for row in csv_f: # li1.append(row[5]) # li1.append(row[0]) severity = int(row[0]) #Has The the intege

我想使用python解析CSV文件,并只输出具有特定值的特定行。这是我到现在为止的代码

import csv  

f = open('alerts2.csv')
csv_f = csv.reader(f)
li1 = []
header = next(csv_f)
for row in csv_f:

    # li1.append(row[5])
    # li1.append(row[0]) 
    severity = int(row[0]) #Has The the integer value from 10 - 40
    Status = str(row[1])
    PolicyName = str(row[2])
    PolicyBlockName = str(row[3])
    PolicyRuleName = str(row[4])
    Summary = str(row[5])
    li1.append(severity)
    li1.append(Summary) # string variables
print li1
f.close()
这将输出severity和summary的所有值,但我希望它仅在severity值为“10”时输出severity和summary的数据。
我想使用列表“li1”搜索列表,如果找到值“10”,则输出值。有什么建议吗??我是python新手。

只需将此检查添加到csv行上的循环:

for row in csv_f:
    severity = int(row[0])
    if severity != 10:
        continue

如果
严重性
值不是10,则循环将
继续
下一行,并且不会对当前行执行任何后续操作。

谢谢,但您可以再解释一下您的解决方案吗。谢谢。python文档中有一节介绍。希望有帮助!
import pandas as pd

alerts_df = pd.DataFrame.from_csv('alerts2.csv', index_col=None)
print alerts_df[alerts_df['severity'] == 10]['Summary']