我希望你们这些好人可以帮助我开展一个项目。本质上,我正在尝试创建一个类,该类将输入CSV文件,检查文件中的数据列数,并将该数据存储在字典中的键值对中。我到目前为止的代码如下:
import csv
class DataStandard():
'''class to store and examine columnar data saved as a csv file'''
def __init__(self, file_name):
self.file_name = file_name
self.full_data_set = {}
with open(self.file_name) as f:
reader = csv.reader(f)
# get labels of each column in list format
self.col_labels = next(reader)
# find the number of columns of data in the file
self.number_of_cols = len(self.col_labels)
# initialize lists to store data using column label as key
for label in self.col_labels:
self.full_data_set[label] = []
我遇到困难的部分是创建字典(full_data_set)后我不知道如何遍历CSV文件的其余部分并将数据存储在每个键的相应值中(柱)。到目前为止我尝试过的所有东西都没有用,因为我必须遍历csv.reader对象。 我希望这个问题有道理,但请随意提出任何澄清问题。此外,如果你想到一种可能以更好的更加pythonic方式工作的方法,我会很感激输入。这是我在课堂上第一个自我引导的项目之一,所以这个主题对我来说是相当新的。提前谢谢!
答案 0 :(得分:0)
要阅读行,您可以使用for row in reader
data = []
with open('test.csv') as f:
reader = csv.reader(f)
headers = next(reader)
for row in reader:
d = dict(zip(headers, row))
#print(d)
data.append(d)
print('data:', data)
如上所述@ PM2Ring csv
有DictReader
with open('test.csv') as f:
reader = csv.DictReader(f)
data = list(reader)
print('data:', data)
答案 1 :(得分:0)
这可能会为您提供解决方案的想法。假设标签仅在第1行,其余为数据,然后在没有数据时行长度变为0:
import csv
class DataStandard():
'''class to store and examine columnar data saved as a csv file'''
def __init__(self, file_name):
self.file_name = file_name
self.full_data_set = {}
#modify method to the following:
with open(self.file_name) as f:
reader = csv.reader(f)
for row in reader:
if row = 0:
# get labels of each column in list format
self.col_labels = next(reader)
# find the number of columns of data in the file
self.number_of_cols = len(self.col_labels)
# initialize lists to store data using column label as key
for label in self.col_labels:
self.full_data_set[label] = []
else:
if len(row) != 0:
for i in range(self.number_of_cols):
label = self.col_labels[i]
self.full_data_set[label] = next(reader)
...我唯一担心的是,虽然'open(...)'有效,但根据我的经验,可以忽略某些级别的缩进。在这种情况下,为了减少缩进的数量,我只需将'row = 0'和'row!= 0'操作分成'with open(...)'的不同实例,即执行第1行,关闭,再次打开,做第2行。