英文:
Read .csv file with columns of varying length as dictionary in Python
问题
# 读取 csv 文件并将其转换为字典
with open(path, 'rt') as csv_file:
reader = csv.reader(csv_file)
header = next(reader) # 读取首行作为字典的键
in_dict = {col: [] for col in header} # 创建一个空字典,以首行为键
for row in reader:
for col, value in zip(header, row):
in_dict[col].append(value)
csv_file.close()
英文:
How do I read in a .csv file in Python with columns of varying lengths? I want to create a dictionary from the .csv file, with the .csv columns as lists of dictionary values.
I've figured out how to write the dictionary to a .csv file, but I need help reading in that same file.
import csv
import itertools
path = 'C:/Users/.../test.csv'
out_dict = {
'Class1': ['A', 'B'],
'Class2': ['C', 'D', 'E', 'F', 'G', 'H', 'I'],
'Class3': ['J', 'K', 'L', 'M', 'N']}
# write dictionary to csv
with open(path, 'wt', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(out_dict.keys())
writer.writerows(itertools.zip_longest(*out_dict.values()))
csv_file.close()
# read csv as dictionary
with open(path, 'rt') as csv_file:
reader = csv.reader(csv_file);
in_dict = ???
csv_file.close()
print(in_dict)
Desired Output:
{'Class1': ['A', 'B'],
'Class2': ['C', 'D', 'E', 'F', 'G', 'H', 'I'],
'Class3': ['J', 'K', 'L', 'M', 'N']}
答案1
得分: 3
要读取CSV文件,我建议使用csv.DictReader
:
import csv
import itertools
path = '<PATH>'
out_dict = {
"Class1": ["A", "B"],
"Class2": ["C", "D", "E", "F", "G", "H", "I"],
"Class3": ["J", "K", "L", "M", "N"],
}
# 将字典写入CSV文件
with open(path, 'wt', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(out_dict.keys())
writer.writerows(itertools.zip_longest(*out_dict.values()))
# 以字典形式读取CSV文件
out = {}
with open(path, 'rt') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
for k, v in row.items():
if v != '':
out.setdefault(k, []).append(v)
print(out)
打印输出:
{
"Class1": ["A", "B"],
"Class2": ["C", "D", "E", "F", "G", "H", "I"],
"Class3": ["J", "K", "L", "M", "N"],
}
英文:
To read the CSV file back I recommend to use csv.DictReader
:
import csv
import itertools
path = '<PATH>'
out_dict = {
"Class1": ["A", "B"],
"Class2": ["C", "D", "E", "F", "G", "H", "I"],
"Class3": ["J", "K", "L", "M", "N"],
}
# write dictionary to csv
with open(path, 'wt', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(out_dict.keys())
writer.writerows(itertools.zip_longest(*out_dict.values()))
# read csv as dictionary
out = {}
with open(path, 'rt') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
for k, v in row.items():
if v != '':
out.setdefault(k, []).append(v)
print(out)
Prints:
{
"Class1": ["A", "B"],
"Class2": ["C", "D", "E", "F", "G", "H", "I"],
"Class3": ["J", "K", "L", "M", "N"],
}
答案2
得分: 1
如Andrej所说,DictReader
可能是一个不错的选择。我提出了一种稍微不同的方法:
in_dict = {}
with open('test.csv', 'r') as f:
reader = csv.DictReader(f)
for row in reader:
for key, val in row.items():
if key not in in_dict.keys():
in_dict[key] = []
if row[key]:
in_dict[key].append(val)
f.close()
print(in_dict)
英文:
As Andrej said, DictReader
is probably the way to go. I came up with a slightly different method:
in_dict = {}
with open('test.csv', 'r') as f:
reader = csv.DictReader(f)
for row in reader:
for key, val in row.items():
if key not in in_dict.keys():
in_dict[key] = []
if row[key]:
in_dict[key].append(val)
f.close()
print(in_dict)
答案3
得分: 0
你也可以使用 json 来将你的字典写入文件,以便以后读取。
from pprint import pprint
import json
out_dict = {
'Class1': ['A', 'B'],
'Class2': ['C', 'D', 'E', 'F', 'G', 'H', 'I'],
'Class3': ['J', 'K', 'L', 'M', 'N']}
with open('test01.json', 'w') as fout:
json.dump(out_dict, fout)
with open('test01.json', 'r') as fin:
in_dict = json.load(fin)
pprint(in_dict)
打印输出:
{'Class1': ['A', 'B'],
'Class2': ['C', 'D', 'E', 'F', 'G', 'H', 'I'],
'Class3': ['J', 'K', 'L', 'M', 'N']}
英文:
You could also use json to write your dict to a file and to later read it.
from pprint import pprint
import json
out_dict = {
'Class1': ['A', 'B'],
'Class2': ['C', 'D', 'E', 'F', 'G', 'H', 'I'],
'Class3': ['J', 'K', 'L', 'M', 'N']}
with open('test01.json', 'w') as fout:
json.dump(out_dict, fout)
with open('test01.json', 'r') as fin:
in_dict = json.load(fin)
pprint(in_dict)
Prints:
{'Class1': ['A', 'B'],
'Class2': ['C', 'D', 'E', 'F', 'G', 'H', 'I'],
'Class3': ['J', 'K', 'L', 'M', 'N']}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论