Created
December 13, 2013 20:27
-
-
Save pfigue/7950818 to your computer and use it in GitHub Desktop.
Generator to read a CSV file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| def rows_from_a_csv_file(filename, skip_first_line=False, dialect='excel', **fmtparams): | |
| with open(filename, 'r') as csv_file: | |
| reader = csv.reader(csv_file, dialect, **fmtparams) | |
| if skip_first_line: | |
| next(reader, None) | |
| for row in reader: | |
| yield row | |
| ## Example: | |
| import sys | |
| if __name__ == '__main__': | |
| csv_file_path = sys.argv[1] | |
| for row in rows_from_a_csv_file(csv_file_path, skip_first_line=True): | |
| print row[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment