Created
November 6, 2017 22:27
-
-
Save mattdconnell/24fe1c0dbb07ad5cad2f054a8a54022e to your computer and use it in GitHub Desktop.
Column retrieval, python, csv
This file contains 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
"""Col dot py | |
Usage: | |
col.py -f file -c column | |
Options: | |
-h help Get help | |
-f file Filename to parse | |
-c column The column to return. | |
""" | |
import csv | |
from docopt import docopt | |
if __name__ == '__main__': | |
args = docopt(__doc__) | |
# print(args) | |
# Open the file specified by -f | |
with open(args['-f'], 'rb') as infile: | |
# Read into csv handler | |
reader = csv.reader(infile, delimiter=',', quotechar='"') | |
# Pop off the first line to get the headers! | |
headers = next(reader) | |
# Get the index of the desired value. | |
position = headers.index(args['-c']) | |
# Iterate over all remaining lines, fetching value at positon | |
for row in reader: | |
print row[position] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment