将数据结构转换为csv

时间:2017-09-14 18:07:03

标签: python python-2.7 csv

我有一个如下所示的数据结构,我需要将其转换为csv文件。我希望以某种方式订购列。

输入数据结构

{
       "total": {
              "a": 300, 
              "c": 300, 
              "b": 300, 
              "e": 300, 
              "d": 300
       }, 
       "japan": {
              "a": 12, 
              "c": 130, 
              "b": 223, 
              "e": 10, 
              "d": 51
       }, 
       "america": {
              "a": 1, 
              "c": 10, 
              "b": 2, 
              "e": 20, 
              "d": 5
       }, 
       "china": {
              "a": 1, 
              "c": 5, 
              "b": 23, 
              "e": 11, 
              "d": 9
       }
}

所需的输出

category,total,japan,china,america
a,300,12,1,1
b,300,223,23,2
c,300,130,5,10
d,300,51,9,5
e,300,10,11,20
cat_total,1500,426,49,87
percentage,,28.4,0.032,0.058

百分比公式(例如日本)

percentage = 426/1500 * 100
where 426 is cat_total of Japan and 1500 is cat_total of total column

我能够提出的代码如下所示

import csv
import sys

my_dict = { 'america' : { 'a' : 1,
                             'b' : 2,
                             'c' : 10,
                             'd' : 5,
                             'e' : 20
                            },
            'japan' : { 'a' : 12,
                             'b' : 223,
                             'c' : 130,
                             'd' : 51,
                             'e' : 10
                            },
            'total': {'a': 300,
                      'b': 300,
                      'c': 300,
                      'd': 300,
                      'e': 300,
                      },
            'china': {'a': 1,
                      'b': 23,
                      'c': 5,
                      'd': 9,
                      'e': 11
                      },

            }


category = set([c for country in my_dict for c in my_dict[country].keys()])


data_to_write = list()

for cat in category:
    temp = list()
    temp.append(cat)
    for country_name in my_dict:
        temp.append(my_dict[country_name][cat])
    data_to_write.append(temp)

csvfile = csv.writer(sys.stderr)
for row in data_to_write:
    csvfile.writerow(row)

我的要求

  1. 需要计算cat_total和百分比行。
  2. 我希望将类别,总数和日本作为csv的前三列,并且对于剩余列的任何订单都可以。
  3. 如果是行,我希望cat_total作为倒数第二列,百分比作为最后一列。
  4. 如果有人可以帮助我,那就太好了。感谢

3 个答案:

答案 0 :(得分:1)

使用pandas(您在使用数据/表时最好的朋友)。

import pandas as pd

d = {
       "total": {
              "a": 300, 
              "c": 300, 
              "b": 300, 
              "e": 300, 
              "d": 300
       },
       # ... etc.
}

# Create DataFrame from dictionary (and transpose it)
df = pd.DataFrame.from_dict(d).T

# Add new columns
df['cat_total'] = df.sum(axis=1)
df['percentage'] = df.sum(axis=1) *100 / df.T.total.sum()

# Transpose it again so we get the index right
df = df.T

# Save your DataFrame as csv 
df.to_csv('data.csv', index_label='category')
print(df) # Or print it ..

安装时只需运行

pip install pandas

答案 1 :(得分:1)

下面是一个纯python解决方案,然后可以像你已经那样传递给csv编写器。

# Get complete set of all keys used in sub-dictionaries (e.g. 'a', 'b', 'c', 'd' and 'e').
subkeys = set()
for k in d:
    subkeys.update(d[k].keys())
subkeys = sorted(subkeys)

# Get columns in desired order.
cols = ['category', 'total', 'japan']
cols += [k for k in d if k not in cols]

# Arrange data as a matrix.
data = [[k] + [d[col].get(k, 0) for col in cols[1:]] for k in subkeys]

# Add totals for each column.
data.append(['cat_total'] + [sum(row[col] for row in data) 
                             for col in range(1, len(d.keys()) + 1) ])

# Calculate percentage of total.
factor = 100. / data[-1][1]
data.append(['percentage', ''] + [round(n * factor, 3) for n in data[-1][2:]])

data_to_write = data
>>> data_to_write
[['a', 300, 12, 1, 1],
 ['b', 300, 223, 23, 2],
 ['c', 300, 130, 5, 10],
 ['d', 300, 51, 9, 5],
 ['e', 300, 10, 11, 20],
 ['cat_total', 1500, 426, 49, 38],
 ['percentage', '', 28.4, 3.267, 2.533]]

答案 2 :(得分:0)

import json
import csv

with open("data.json") as file:
    data = json.load(file)

with open("data.csv", "w") as file:
    csv_file = csv.writer(file)
    for item in data:
      //write your json data here