0

Assuming I have the following CSV file:

User ID   Name        Application  

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

Is there some easy way to (import this into some data structure)? and/or be able to easily perform the following operations in 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?

Is there some python library or what is the easiest way to accomplish the above?

1
  • 1
    Have you looked at the csv module? Commented Oct 30, 2012 at 4:47

1 Answer 1

1

Trivial using DictReader. You need to pass excel-tab as the dialect since your fields are tab delimited.

rows is a list of dictionaries.

>>> 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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.