简体   繁体   English

如何从CSV文件读取多个记录?

[英]How to read multiple records from a CSV file?

I have a csv file, l__cyc.csv , that contains this: 我有一个csv文件l__cyc.csv ,其中包含以下内容:

trip_id, time, O_lat, O_lng, D_lat, D_lng
130041910101,1300,51.5841153671,0.134444590094,51.5718053872,0.134878021928
130041910102,1335,51.5718053872,0.134878021928,51.5786920389,0.180940040247
130041910103,1600,51.5786920389,0.180940040247,51.5841153671,0.134444590094
130043110201,1500,51.5712712038,0.138532882664,51.5334949484,0.130489470325
130043110202,1730,51.5334949484,0.130489470325,51.5712712038,0.138532882664

And I am trying to pull out separate values, using: 我正在尝试使用以下方法提取单独的值:

with open('./l__cyc.csv', 'rU') as csvfile:
    reader = csv.DictReader(csvfile)
    origincoords = ['{O_lat},{O_lng}'.format(**row) for row in reader]
with open('./l__cyc.csv', 'rU') as csvfile:
    reader = csv.DictReader(csvfile)
    trip_id = ['{trip_id}'.format(**row) for row in reader]
with open('./l__cyc.csv', 'rU') as csvfile:
    reader = csv.DictReader(csvfile)
    destinationcoords = ['{D_lat},{D_lng}'.format(**row) for row in reader]

Where origincoords should be 51.5841153671, 0.134444590094 , 如果origincoords应为51.5841153671, 0.134444590094
trip_id should be 130041910101 , and destinationcoords should be trip_id应该是130041910101 ,而destinationcoords应该是
51.5718053872, 0.134878021928 . 51.5718053872, 0.134878021928

However, I get a KeyError : 但是,我得到一个KeyError

KeyError: 'O_lat'

Is this something simple and there's something fundamental I'm misunderstanding? 这是简单的事情,是我误会了一些基本问题吗?

You just avoid the space between headers 您只需要避免标题之间的空格

trip_id,time,O_lat,O_lng,D_lat,D_lng 

OR 要么

reader = csv.DictReader(csvfile, skipinitialspace=True)

First things first, you get the key error, because the key does not exist in your dictionary. 首先,您会得到密钥错误,因为密钥在您的词典中不存在。

Next, I would advise against running through the file 3 times, when you can do it a single time! 接下来,我建议不要一次运行文件3次,而只需一次即可!

For me it worked, when I added the fieldnames to the reader. 对我来说,当我将字段名添加到阅读器时,它起作用了。

import csv
from cStringIO import StringIO

src = """trip_id, time, O_lat, O_lng, D_lat, D_lng
130041910101,1300,51.5841153671,0.134444590094,51.5718053872,0.134878021928
130041910102,1335,51.5718053872,0.134878021928,51.5786920389,0.180940040247
130041910103,1600,51.5786920389,0.180940040247,51.5841153671,0.134444590094
130043110201,1500,51.5712712038,0.138532882664,51.5334949484,0.130489470325
130043110202,1730,51.5334949484,0.130489470325,51.5712712038,0.138532882664
"""
f = StringIO(src)

# determine the fieldnames
fieldnames= "trip_id,time,O_lat,O_lng,D_lat,D_lng".split(",")

# read the file
reader = csv.DictReader(f, fieldnames=fieldnames)

# storage
origincoords = []
trip_id = []
destinationcoords = []

# iterate the rows
for row in reader:
    origincoords.append('{O_lat},{O_lng}'.format(**row))
    trip_id.append('{trip_id}'.format(**row))
    destinationcoords.append('{D_lat},{D_lng}'.format(**row))

# pop the header off the list
origincoords.pop(0)
trip_id.pop(0)
destinationcoords.pop(0)

# show the result
print origincoords
print trip_id
print destinationcoords

I don't really know what you are trying to achieve there, but I'm sure there is a better way of doing it! 我真的不知道您要在此达成什么目标,但是我敢肯定有更好的方法来做到这一点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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