【问题标题】:How would I write to a CSV file from a python dictionary of dictionaries while some values are empty在某些值为空的情况下,如何从 python 字典字典写入 CSV 文件
【发布时间】:2016-05-20 20:35:47
【问题描述】:

我有一个 Python 字典,并存储了我需要写入 CSV 文件的日期。

我遇到的问题是我读过的文件中的一些字典不包含该特定 ID 的任何信息。所以我的 CSV 文件列没有正确排列。

例子

 d["first1"]["title"] = founder
  d["first1"]["started"] = 2005
  d["second1"]["title"] = CEO
  d["second1"]["favcolour"] = blue

所以当我使用以下代码时:

for key, value in d.iteritems():
 ln = [key]
        for ikey, ivalue in value.iteritems():
           ln.append(ikey)
           ln.extend([v for v in ivalue])
        writer.writerow(ln)

我的 CSV 文件将包含所有信息,但“started”和“favcolour”在我想要的同一列中,因此这些列仅包含一个。

提前谢谢大家

【问题讨论】:

  • 您能展示您当前和预期的输出格式吗?

标签: python python-2.7 csv dictionary


【解决方案1】:

这里有一个建议:

d = {"first1": {"title": 'founder', "started": 2005}, "second1": {"title": 'CEO', "favcolour": 'blue'}}

columns = []
output = []
for key, value in d.iteritems():
    for ikey, ivalue in value.iteritems():
        if ikey not in columns:
            columns.append(ikey)
    ln = []
    for col in columns:
        if col not in value:
            ln.append('')
        else:
            ln.append(value[col])

    output.append(ln)

with open('file', 'w') as fl:
    csv_writer = csv.writer(fl)
    csv_writer.writerow(columns)
    for ln in output:
        print ln
        csv_writer.writerow(ln)

文件:

started,title,favcolour
2005,founder
,CEO,blue

【讨论】:

    【解决方案2】:

    如果它不需要是人类可读的,你也可以使用pickle:

    import pickle
    
    # Write:
    with open('filename.pickle', 'wb') as handle:
      pickle.dump(d, handle)
    
    # Read:
    with open('filename.pickle', 'rb') as handle:
      d = pickle.load(handle)
    

    【讨论】:

      【解决方案3】:

      您可以使用csv 中的DictWriter 类轻松地将稀疏字典附加到CSV 中。唯一需要注意的是,您需要在开始时了解所有可能的字段。

      import csv
      
      data = { "first": {}, "second": {} }
      data["first"]["title"] = "founder"
      data["first"]["started"] = 2005
      data["second"]["title"] = "CEO"
      data["second"]["favcolour"] = "blue"
      
      
      fieldNames = set()
      
      for d in data:
        for key in data[d].keys():
          # Add all possible keys to fieldNames, beacuse fieldNames is
          # a set, you can't have duplicate values
          fieldNames.add(key)
      
      
      with open('csvFile.csv', 'w') as csvfile:
        # Initialize DictWriter with the list of fieldNames
        # You can sort fieldNames to whatever order you wish the CSV
        # headers to be in.
        writer = csv.DictWriter(csvfile, fieldnames=list(fieldNames))
      
        # Add Header to the CSV file
        writer.writeheader()
      
        # Iterate through all sub-dictionaries
        for d in data:
          # Add the sub-dictionary to the csv file
          writer.writerow(data[d])
      

      【讨论】:

        【解决方案4】:

        Pandas 非常适合这类事情,所以如果可以的话,我会推荐它。

        import pandas as pd
        
        #not necessary, but for me it's usually easier to work with a list of dicts than dicts
        my_list = [my_dict[key] for key in my_dict]
        # When you pass a list of dictionaries to pandas DataFrame class, it will take care of 
        #alignment issues for you, but if you're wanting to do something specific 
        #with None values, you will need to further manipulate the frame
        df = pd.DataFrame(my_list)
        df.to_csv('file_path_to_save_to')
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-11
          相关资源
          最近更新 更多