-
-
Save bycoffe/187278 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env python | |
| """ | |
| Like cut, but for CSVs. To be used from a shell command line. | |
| Change row[1] to the row index to be printed. row[1] will print the second | |
| item in the row. | |
| Leveraged from an example from @bycoffe | |
| """ | |
| import sys, csv, getopt | |
| opts, args = getopt.getopt(sys.argv[1:], "f:", ["fields="]) | |
| if args: | |
| i = open(args[0]) | |
| else: | |
| i = sys.stdin | |
| if opts: | |
| cols = opts[0][1].split(",") | |
| else: | |
| cols = [1] | |
| for row in csv.reader(i): | |
| for c in cols: | |
| print row[int(c)], | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment