假设我有以下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库或者最简单的方法是什么?
答案 0 :(得分: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']