Skip to content

Instantly share code, notes, and snippets.

@zstumgoren
Created April 9, 2011 18:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zstumgoren/911615 to your computer and use it in GitHub Desktop.
Save zstumgoren/911615 to your computer and use it in GitHub Desktop.
Sample script on how to use Python's csv DictReader class to process tabular data.
#!/usr/bin/env python
import csv
source_file = open('/path/to/Testfile.txt','rb')
# Use the DictReader class to read in your data. This class allows you to access
# field names by name. It assumes the first line in the file contains the field names
for line in csv.DictReader(source_file, delimiter='\t'):
net_income = line['net_income'].strip().replace('(','-').replace(')','')
bank_name = line['name'].strip()
# process other fields as necessary, and then print them to shell
print bank_name, net_income
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment