此代码会打开一个CSV,读取并打印'标题后的第一行。行。我使用pprint来获取字典的输出。但是,即使使用print(),我的代码也不会打印任何内容。我哪里错了?
import csv
from datetime import datetime
from pprint import PrettyPrinter
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)
def first_trip (trip_reader):
reader = csv.DictReader (trip_reader)
first = next (reader)
return first
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(first_trip)