开发者社区> 问答> 正文

如何快速将csv表导出到python词典?

将csv表导出到python词典最简单的方式是什么?

展开
收起
游客6qcs5bpxssri2 2019-09-22 17:09:43 1377 0
1 条回答
写回答
取消 提交回答
  • 推荐回答

    import csv

    with open('csv_file.csv', 'r') as csv_file:

    csv_reader = csv.reader(csv_file)
    
    compound_dictionary = dict()
    
    last_compound_key = str()
    
    for row in csv_reader:
    
        if row[0] != '':
    
            last_compound_key = row[0]
    
            compound_dictionary[last_compound_key] = dict()
    
    
        compound_dictionary[last_compound_key][row[1]] = row[2]
    

    print(compound_dictionary)

    2019-09-22 17:10:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载