Python 如何从CSV文件中读取多条记录?

Python 如何从CSV文件中读取多条记录?,python,csv,Python,Csv,我有一个csv文件l_ucyc.csv,其中包含以下内容: 跳闸id、时间、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.18

我有一个csv文件l_ucyc.csv,其中包含以下内容:

跳闸id、时间、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 我正在尝试使用以下方法提取单独的值:

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]
其中origincoords应为51.5841153671,0.134444590094, trip_id应为13004191101,DestinationWord应为 51.5718053872,0.134878021928

但是,我得到一个关键错误:

键错误:“O_lat”
这是一个简单的问题,而我误解了一些基本的问题吗?

您只需避免标题之间的空格即可

trip_id,time,O_lat,O_lng,D_lat,D_lng 


您只需避免标题之间的空格

trip_id,time,O_lat,O_lng,D_lat,D_lng 


首先,您会得到密钥错误,因为您的字典中不存在密钥

接下来,我建议不要在文件中运行3次,因为您可以只运行一次

对我来说,当我将字段名添加到阅读器中时,它起到了作用

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

我真的不知道你想在那里实现什么,但我相信有更好的方法

首先,您会得到密钥错误,因为您的字典中不存在密钥

接下来,我建议不要在文件中运行3次,因为您可以只运行一次

对我来说,当我将字段名添加到阅读器中时,它起到了作用

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

我真的不知道你想在那里实现什么,但我相信有更好的方法

不幸的是没有。这个错误对我很有用。请注意,D_lng之后也不得有尾随空间。问题中粘贴在上面的代码在D_lng之后有一个空格。不幸的是,没有。这个错误对我很有用。请注意,D_lng之后也不得有尾随空间。问题上面粘贴的代码在D_lng后面有一个空格。谢谢@enpenax。你说得对,三次迭代是不必要的。非常感谢您的代码和有用的注释。我在学习;谢谢@enpenax。你说得对,三次迭代是不必要的。非常感谢您的代码和有用的注释。我在学习;