繁体   English   中英

使用DictReader对象从CSV文件中提取字典

[英]Dictionary from CSV file using the DictReader object

要设置上下文,我需要根据我的要求转换一个csv文件,以创建转换后的csv输出文件。

对于此问题陈述,我了解到我需要使用来自python csv的DictReader 转换后,我尝试创建另一个名为new_point的字典,该字典需要保留到csv文件中。

问题是,字典生成似乎在我的代码中存在缺陷,因为当我打印字典时,它既不是字典数组也不是字典。

with open(out_file, 'w') as f_out, open(in_file, 'r') as f_in:
        out_colnames = ['duration', 'month', 'hour', 'day_of_week', 'user_type']        
        trip_writer = csv.DictWriter(f_out, fieldnames = out_colnames)
        trip_writer.writeheader()

        trip_reader = csv.DictReader(f_in)

        # collect data from and process each row
        for row in trip_reader:
            # set up a dictionary to hold the values for the cleaned and trimmed
            # data point
            new_point = {}
            ## TODO: use the helper functions to get the cleaned data from  ##
            ## the original data dictionaries.                              ##
            ## Note that the keys for the new_point dictionary should match ##
            ## the column names set in the DictWriter object above.         ##
            duration = duration_in_mins(row, city)
            month,hour,day_of_week = time_of_trip(row, city)
            user_type = type_of_user(row, city)

            new_point['duration'] = duration
            new_point['month'] = month
            new_point['hour'] = hour
            new_point['day_of_week'] = day_of_week
            new_point['user_type'] = user_type

            pprint(new_point)

如果我打印type(new_point)我得到的输出如下

Class <Dict>

当我打印new_point字典时,我得到以下内容。

{'day_of_week': 'Thursday',
 'duration': 7.123116666666666,
 'hour': 22,
 'month': 3,
 'user_type': 'Registered'}
{'day_of_week': 'Thursday',
 'duration': 9.792516666666668,
 'hour': 22,
 'month': 3,
 'user_type': 'Registered'}
{'day_of_week': 'Thursday',
 'duration': 6.632983333333333,
 'hour': 22,
 'month': 3,
 'user_type': 'Registered'}
{'day_of_week': 'Thursday',
 'duration': 7.4047,
 'hour': 22,
 'month': 3,
 'user_type': 'Registered'}
{'day_of_week': 'Thursday',
 'duration': 13.014583333333333,
 'hour': 22,
 'month': 3,
 'user_type': 'Registered'}
{'day_of_week': 'Thursday',
 'duration': 17.0552,
 'hour': 22,
 'month': 3,
 'user_type': 'Registered'}
{'day_of_week': 'Thursday',
 'duration': 11.01165,
 'hour': 22,
 'month': 3,
 'user_type': 'Registered'}
{'day_of_week': 'Thursday',
 'duration': 30.01175,
 'hour': 21,
 'month': 3,
 'user_type': 'Registered'}

有人已经采用了从DictReader对象创建字典的方法,可以帮助我解决此问题。

如果您发现这个问题非常幼稚,我诚挚的歉意。 我对python完全陌生。 我尝试阅读文档,但不了解该概念。 我什至在任何地方都找不到详细的解释。

从我看到的new_point来看, new_point看起来不错:它是一本字典,其键与列标题相同。 但是,我看不到任何写入输出的语句。 构建字典后如何写出来呢?

        new_point['duration'] = duration
        new_point['month'] = month
        new_point['hour'] = hour
        new_point['day_of_week'] = day_of_week
        new_point['user_type'] = user_type
        trip_writer.writerow(new_point)  # Write to the file

暂无
暂无

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

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