【发布时间】:2017-06-21 20:51:53
【问题描述】:
我想使用 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()
这会输出严重性和摘要中的所有值,但我希望它仅在严重性值为 "10" 时才输出严重性和摘要的数据。 我正在考虑使用列表“li1”并搜索列表,如果找到值“10”,则输出这些值。有什么建议么??我是python新手。
【问题讨论】:
标签: python python-2.7 csv parsing