Reputation: 12316
I have some parsing scripts which accept input from either a file or stdin
and parse them using pandas read_csv
, which takes a file-like object as its input.
Sometimes the header differs, so I had it set up with try-except to try reading in the file, but if it fails to parse, then try with an alternative header specification.
This works for files, but if I use stdin
as the input, it seems that the header line gets "consumed" by the first attempt at read_csv
.
I am wondering if there is a way to buffer this line, or reset stdin
for a second attempt at parsing, in a way that pandas will like.
Not sure example code will help, but:
if Opt.Args:
FileName = Opt.Args[0]
print >> sys.stderr, "READING from FILE",FileName
elif not sys.stdin.isatty():
print >> sys.stderr, "READING from STDIN"
FileName = sys.stdin
try:
df = pd.read_csv(FileName,header=0,sep="\t", parse_dates=['RecordedDate'])
except ValueError:
try:
df = pd.read_csv(FileName,header=0,sep="\t",parse_dates=['RecordedDate_10'])
...etc...
Upvotes: 2
Views: 2164
Reputation: 656
There's not one perfect way to "reset" stdin, at least not one which works across different operating systems. You could however try "rebinding" STDIN and it practically clears it.
on unixoid systems, something like this could work:
import sys
text = sys.stdin.read()
sys.stdin = open("/dev/tty") sel = raw_input("Selection? : ")
Refer: http://www.gossamer-threads.com/lists/python/python/467361
Upvotes: 3