Skip to content

Instantly share code, notes, and snippets.

@ishu3101
Created July 6, 2016 01:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishu3101/5b205f5552794593fcff34c280f1d5fa to your computer and use it in GitHub Desktop.
Save ishu3101/5b205f5552794593fcff34c280f1d5fa to your computer and use it in GitHub Desktop.
Read CSV data as Input either using STDIN or as Command Line Argument using Python
import sys
import csv
try:
if (sys.argv[1] == '-'):
f = sys.stdin.read().splitlines()
else:
filename = sys.argv[1]
f = open(filename, 'r')
csv = csv.reader(f)
data = list(csv)
for row in data:
print row
except Exception, e:
print "Error Reading from file:"
abc efg hij
klm nop qrs
tuv wxy zab

Usage

python read_args.py test.csv
cat test.csv | python read_args.py -
python read_args.py -

Output

['abc', 'efg', 'hij']
['klm', 'nop', 'qrs']
['tuv', 'wxy', 'zab']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment