【问题标题】:Read csv to dict of lists of dicts读取 csv 到字典列表的字典
【发布时间】:2017-12-17 20:42:31
【问题描述】:

我有一个包含大量(有意)重复的数据集。我想折叠(?)以使其更适合我的需求。数据如下所示:

Header1, Header2,  Header3
Example1, Content1, Stuff1
Example1, Content2, Stuff2
Example1, Content3, Stuff3
Example2, Content1, Stuff1
Example2, Content5, Stuff5
etc...

我希望它最终成为一个字典,其中一列的值作为键,字典列表作为这些键的值,如下所示:

{Example1 : [{Header2:Content1, Header3:Stuff1}, {Header2:Content2, Header3:Stuff2}, {Header2:Content3, Header3:Stuff3}],
 Example2 : [{Header2:Content1, Header3:Stuff1}, {Header2:Content5, Header3:Stuff5}]}

我是 Python 的新手,并且是一个新手程序员,所以如果这个问题令人困惑,请随时澄清。 ???谢谢!

更新 我因未发布示例代码而受到指责(感谢您让我诚实!)所以就在这里。下面的代码有效,但由于我是 Python 新手,我不知道它是否写得好。字典也以相反的顺序结束了键(Example1 和 Example2)。这并不重要,但我不明白为什么。

def gather_csv_info():
    all_csv_data = []
    flattened_data = {}
    reading_csv = csv.DictReader(open(sys.argv[1], 'rb'))

    for row in reading_csv:
        all_csv_data.append(row)

    for row in all_csv_data:
        if row["course_sis_ids"] in flattened_data:
            flattened_data[row["course_sis_ids"]].append({"user_sis_ids":row["user_sis_ids"], "file_ids":row["file_ids"]})
        else:
            flattened_data[row["course_sis_ids"]] = [{"user_sis_ids":row["user_sis_ids"], "file_ids":row["file_ids"]}]

    return flattened_data

【问题讨论】:

  • 我没有看到任何代码。您尝试了什么,为什么它不起作用?
  • 你的问题太宽泛了,特别是没有任何代码来显示上下文。
  • 好久没发帖了,完全忘记了自己的礼仪。感谢@MikeScotty 的那个网站,太棒了。
  • @lyonsinbeta 不客气。我已经删除了我的反对票。干杯 =)

标签: python csv dictionary


【解决方案1】:

这段代码有效,但我不知道它是多么pythonic,我不明白为什么flattened_data dict 的键与它们出现在原始CSV 中的顺序相反。严格来说,它们不按顺序排列并不重要,但这很奇怪。

def gather_csv_info():
    all_csv_data = []
    flattened_data = {}
    reading_csv = csv.DictReader(open(sys.argv[1], 'rb'))

    for row in reading_csv:
        all_csv_data.append(row)

    for row in all_csv_data:
        if row["course_sis_ids"] in flattened_data:
            flattened_data[row["course_sis_ids"]].append({"user_sis_ids":row["user_sis_ids"], "file_ids":row["file_ids"]})
        else:
            flattened_data[row["course_sis_ids"]] = [{"user_sis_ids":row["user_sis_ids"], "file_ids":row["file_ids"]}]

    return flattened_data

【讨论】:

  • 字典不按顺序保存键,这不是字典的用途。如果您需要订单,请改用列表或 OrderedDict。
  • 我不需要它们按顺序排列,我知道键值对的重点是它们不需要按已知顺序排列,我只是评论说我不知道为什么他们按照他们所在的顺序,因为这不是他们被读入字典的顺序。
【解决方案2】:

当你改变你的问题时,我完全改变了答案,所以我只是在你自己的答案中整理了代码,所以它更“Pythonic”:

import csv
from collections import defaultdict

def gather_csv_info(filename):
    all_csv_data = []
    flattened_data = defaultdict(list)
    with open(filename, 'rb') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            key = row["Header1"]
            flattened_data[key].append({"Header2":row["Header2"], "Header3":row["Header3"]})
    return flattened_data

print(gather_csv_info('data.csv'))

不确定您为什么需要这种格式的数据,但这取决于您。

【讨论】:

  • 不发表评论就拒绝投票是没有帮助的,因为仍在学习的人将不得不猜测它为什么不好,并且可能会弄错。
  • 文件是 CSV,为了便于阅读,我只是做了一个简单的表格。我用逗号更新了它,但留下了空格以保持可读性。
  • 不是很有帮助。如果您发布问题,人们会花时间尝试根据您发布的内容为您提供解决方案。如果您将问题编辑为完全不同,那么这些答案会被不公平地否决。而且您还接受了自己的答案,该答案远非示例代码,而且对下一个程序员也没有太大帮助...
  • 如果有人提供更好的答案,或者我想出更好的答案,我会很乐意更改所选答案,但我提供的答案确实解决了这个问题。至少如果有人发现这个问题,他们会看到一个可行的解决方案,并注明这是新手的尝试。
  • 我编辑了我的问题以显示您的代码的改进版本。如果您愿意,您甚至可以将其复制到您的问题中。
猜你喜欢
  • 2015-12-24
  • 2019-05-04
  • 2018-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多