简体   繁体   中英

Read inputfile as csv in python

I want to read a csv file from STDIN and operate on it.

The following is the code for reading the csv file and doing the operation needed. This works fine. But I want to take the input from STDIN.

import csv
with open('hospital_data.csv', 'rb') as csvfile:
    myDict = {}

    csvreader = csv.reader(csvfile, delimiter=',')
    for row in csvreader:
        if row[6] not in myDict.keys():
            #print 'Zipcode: ' + row[6] + ' Hospital code: ' + row[1]
            myDict[row[6]] = 1
        elif row[6] in myDict.keys():
            #print 'value in row '+ str(myDict[row[6]])
            myDict[row[6]] += 1

Is there a way in Python to read the file from STDIN as a csv file ?

csv.reader will take anything that yields lines, so you can use any of the methods shown at this answer to get lines from stdin: How do you read from stdin in Python?

I'm partial to fileinput myself due to its flexibility. EG:

import csv
import fileinput

myDict = {}
csvreader = csv.reader(fileinput.input(mode='rb'), delimiter=',')

But this works too:

import csv
import sys

myDict = {}
csvreader = csv.reader(sys.stdin, delimiter=',')

If you do that, you'll want to run with the -u command line argument to make stream binary, if that makes a difference on your platform: https://docs.python.org/2/using/cmdline.html#cmdoption-u

In either case you'll need to use control-D to mark the end of the input.

Note that the correct way to check if a key is in a dict is if row[6] in myDict rather than checking keys . And in fact if you just want a default value when the key is not present, use get :

for row in csvreader:
    myDict[row[6]] = myDict.get(row[6], 0) + 1

Or look into collections.Counter , since you're on 2.7:

myDict = collections.Counter(row[6] for row in csvreader)

Use sys.stdin, it's file-like object.

import sys
import csv

data = sys.stdin.readlines()
csvreader = csv.reader(data, delimiter=',')
for row in csvreader:
    print row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM