0

假设我有以下 CSV 文件:

User ID   Name        Application  

001       Ajohns      ABI
002       Fjerry      Central
900       Xknight     RFC
300       JollK       QDI
078       Demik       Central

是否有一些简单的方法(将其导入某些数据结构)?和/或能够轻松地在 python 中执行以下操作:

1) Get all user IDs with Application=Central
2) Get the row where name="FJerry" then extract say the "userid" value from
3) Give me the filtered rows for those with "Application=Central" so I can write out to CSV
4) Give me all the rows where Name="Ajohn", and Application="ABI" such that I could easy do a len() to count them up?

是否有一些 python 库或完成上述操作的最简单方法是什么?

4

1 回答 1

1

使用DictReader. 您需要excel-tab作为方言传递,因为您的字段是制表符分隔的。

rows是一个字典列表。

>>> with open(r'd:\file.csv','rb') as f:
...     reader = csv.DictReader(f,dialect='excel-tab')
...     rows = list(reader)
...
>>> [x['User ID'] for x in rows if x['Application'] == 'Central']
['002', '078']
于 2012-10-30T05:12:17.560 回答