【发布时间】:2015-11-24 21:26:08
【问题描述】:
我目前正在尝试找出一种方法来获取跨多个数据集存储为 .csv 文件的信息。
上下文
就本题而言,假设我有 4 个数据集:experiment_1.csv、experiment_2.csv、experiment_3.csv 和experiment_4.csv。在每个数据集中,有 20,000 多行,每行有 80 多列。每行代表一个动物,由一个 ID 号标识,每列代表关于该动物的各种实验数据。假设每一行的动物 ID 号对于每个数据集都是唯一的,但并非在所有数据集中都是唯一的。例如 ID#ABC123 可以在experiment_1.csv、experiment_2.csv 中找到,但不是experiment_3.csv 和experiment_4.csv
问题
假设用户想要通过在所有数据集中查找每个动物的 ID # 来获取大约 100 只动物的信息。我该怎么做呢?我对编程比较陌生,我想改进。这是我目前所拥有的。
class Animal:
def __init__(self, id_number, *other_parameters):
self.animal_id = id_number
self.animal_data = {}
def store_info(self, csv_row, dataset):
self.animal_data[dataset] = csv_row
# Main function
# ...
# Assume animal_queries = list of Animal Objects
# Iterate through each dataset csv file
for dataset in all_datasets:
# Make a copy of the list of queries
copy_animal_queries = animal_queries[:]
with open(dataset, 'r', newline='') as dataset_file:
reader = csv.DictReader(dataset_file, delimiter=',')
# Iterate through each row in the csv file
for row in reader:
# Check if the list is not empty
if animal_queries_copy:
# Get the current row's animal id number
row_animal_id = row['ANIMAL ID']
# Check if the animal id number matches with a query for
# every animal in the list
for animal in animal_queries_copy[:]:
if animal.animal_id == row_animal_id:
# If a match is found, store the info, remove the
# query from the list, and exit iterating through
# each query
animal.store_info(row, dataset)
animal_list_copy.remove(animal)
break
# If the list is empty, all queries were found for the current
# dataset, so exit iterating through rows in reader
else:
break
讨论
有没有更明显的方法呢?假设我现在想使用 .csv 文件,我会考虑将这些 .csv 文件转换为更易于使用的格式,例如 SQL 表(我是数据库和 SQL 的绝对初学者,所以我需要花时间学习这个)。
让我印象深刻的一件事是我必须创建animal_queries 的多个副本:每个数据集1 个,数据集中的每一行1 个(在for 循环中)。由于 1 行仅包含 1 个 ID,因此一旦找到与来自 animal_queries 的 ID 匹配的内容,我就可以提前退出循环。此外,由于已经找到该 ID,我不再需要为当前数据集的其余部分搜索该 ID,因此我将其从列表中删除,但我需要保留查询的原始副本,因为我还需要它来搜索剩余的数据集。但是,我无法在 for 循环内从列表中删除元素,因此我还需要创建另一个副本。这对我来说似乎不是最优的,我想知道我是否在错误的方向上接近这个。任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: python csv python-3.x