我想打印出从3个城市中每个城市记录的第一次旅行(每个数据文件的第二行)以用于自行车共享系统。但是,它仅打印一个城市,并且此错误一直显示:
AttributeError: 'builtin_function_or_method' object has no attribute 'fieldnames'
我试图搜索答案,但找不到任何内容
这是我的代码:
import csv
from datetime import datetime
from pprint import pprint
def print_first_point(filename):
"""
This function prints and returns the first data point (second row) from
a csv file that includes a header row.
"""
# print city name for reference
city = filename.split('-')[0].split('/')[-1]
print('\nCity: {}'.format(city))
with open(filename, 'r') as f_in:
trip_reader = csv.DictReader(f_in)
first_trip = csv.reader.fieldnames
pprint(first_trip)
# output city name and first trip for later testing
return (city, first_trip)
# list of files for each city
data_files = ['./data/NYC-CitiBike-2016.csv',
'./data/Chicago-Divvy-2016.csv',
'./data/Washington-CapitalBikeshare-2016.csv',]
# print the first trip from each file, store in dictionary
example_trips = {}
for data_file in data_files:
city, first_trip = print_first_point(data_file)
example_trips[city] = first_trip
答案 0 :(得分:0)
csv.reader是必须向其传递参数的函数,当前的表示法是寻找csv.reader的变量“ fieldnames”。
例如:csv.reader(fieldnames);
这里是示例链接。 enter link description here