[英]Read inputfile as csv in python
I want to read a csv file from STDIN and operate on it. 我想从STDIN读取一个csv文件并对其进行操作。
The following is the code for reading the csv file and doing the operation needed. 以下是用于读取csv文件并执行所需操作的代码。 This works fine.
这很好用。 But I want to take the input from STDIN.
但我想从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 ? 有没有办法在Python中将文件从STDIN读取为csv文件?
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? csv.reader
将获取产生行的任何内容,因此您可以使用此答案中显示的任何方法从stdin获取行: 如何从Python中读取stdin?
I'm partial to fileinput
myself due to its flexibility. 由于其灵活性,我
fileinput
自己fileinput
。 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 如果你这样做,你将需要使用
-u
命令行参数运行以生成流二进制文件,如果这会对您的平台产生影响: 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. 在任何一种情况下,您都需要使用control-D来标记输入的结尾。
Note that the correct way to check if a key is in a dict is if row[6] in myDict
rather than checking keys
. 请注意,检查密钥是否在dict中的正确方法是
if row[6] in myDict
而不是检查keys
。 And in fact if you just want a default value when the key is not present, use get
: 事实上,如果您只想在密钥不存在时使用默认值,请使用
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: 或者查看
collections.Counter
,因为你在2.7:
myDict = collections.Counter(row[6] for row in csvreader)
Use sys.stdin, it's file-like object. 使用sys.stdin,它是类似文件的对象。
import sys
import csv
data = sys.stdin.readlines()
csvreader = csv.reader(data, delimiter=',')
for row in csvreader:
print row
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.