Read .csv file with columns of varying length as dictionary in Python.

huangapple go评论62阅读模式
英文:

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 = &#39;&lt;PATH&gt;&#39;

out_dict = {
    &quot;Class1&quot;: [&quot;A&quot;, &quot;B&quot;],
    &quot;Class2&quot;: [&quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;],
    &quot;Class3&quot;: [&quot;J&quot;, &quot;K&quot;, &quot;L&quot;, &quot;M&quot;, &quot;N&quot;],
}

# write dictionary to csv
with open(path, &#39;wt&#39;,  newline=&#39;&#39;) 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, &#39;rt&#39;) as csv_file:
    reader = csv.DictReader(csv_file)
    for row in reader:
        for k, v in row.items():
            if v != &#39;&#39;:
                out.setdefault(k, []).append(v)

print(out)

Prints:

{
    &quot;Class1&quot;: [&quot;A&quot;, &quot;B&quot;],
    &quot;Class2&quot;: [&quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;],
    &quot;Class3&quot;: [&quot;J&quot;, &quot;K&quot;, &quot;L&quot;, &quot;M&quot;, &quot;N&quot;],
}

答案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(&#39;test.csv&#39;, &#39;r&#39;) 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 = {
    &#39;Class1&#39;:       [&#39;A&#39;, &#39;B&#39;],
    &#39;Class2&#39;:       [&#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;],
    &#39;Class3&#39;:       [&#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;]}
   
with open(&#39;test01.json&#39;, &#39;w&#39;) as fout:
    json.dump(out_dict, fout)

with open(&#39;test01.json&#39;, &#39;r&#39;) as fin:
    in_dict = json.load(fin)

pprint(in_dict)

Prints:

{&#39;Class1&#39;: [&#39;A&#39;, &#39;B&#39;],
 &#39;Class2&#39;: [&#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;],
 &#39;Class3&#39;: [&#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;]}

huangapple
  • 本文由 发表于 2023年6月13日 06:26:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定