我有一个CSV文件,包含大约30个标题(列)和2000行。
HeaderOne | HeaderTwo | HeaderThree
dataRowOne | dataRowOne | dataRowOne
dataRowTwo | dataRowTwo | dataRowTwo
我想使用Python搜索字符串,然后输出该行。举例来说,例如我搜索'cocaColaIsTheBest'并且它在E2018单元格中,我希望Python打印出2018年的行,其标题位于其上方。
到目前为止,我已经:
import csv
myExSpreadSheet = csv.reader(open('Simon.csv', 'rb'))
for row in myExSpreadSheet:
if 'cocaColaIsTheBest' in row:
print (row)
这将在字典中打印该行;我也希望它打印标题。
如何打印所有标题?
以及如何打印特定的标题?
标题是第一行;首先捕获那些:
with open('Simon.csv', 'rb') as csvfile:
myExSpreadSheet = csv.reader(csvfile)
headers = next(myExSpreadSheet, None) # grab first row
for row in myExSpreadSheet:
if 'cocaColaIsTheBest' in row:
print headers
print row
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句