【发布时间】:2020-11-30 10:35:22
【问题描述】:
我有一个包含以下示例数据的 csv:
id bb_id cc_id datetime
-------------------------
1 11 44 2019-06-09
2 33 55 2020-06-09
3 22 66 2020-06-09
4 11 44 2019-06-09
5 11 44 2020-02-22
假设条件是if bb_id == 11 and cc_id == 44获取最新记录,即:
11 44 2020-02-22
如何从 csv 中获取此信息?
我做了什么:
with open('sample.csv') as csv_file
for indx, data in enumerate(csv.DictReader(csv_file)):
# check if the conditional data is in the file?
if data['bb_id'] == 11 and data['cc_id'] == 44:
# sort the data by date? or should I store all the relevant data before hand in a data structure like list and then apply sort on it? could I avoid that? as I need to perform this interactively multiple times
【问题讨论】:
-
你会考虑使用熊猫吗?
-
这不能使用 csv 模块吗?
-
当然可以。熊猫只是让它变得微不足道。
-
当然,我认为最好先检查
bb和cc值是否存在于 csv 中,如果它们仅查找最新记录?
标签: python python-3.x csv sorting