将dict转换为csv

时间:2019-02-28 04:02:31

标签: python csv

我需要用Python为kafka使用者编写代码。我有主题名称。以及来自主题ia的数据,如下所示:

{"MT":"500","FISC_YR":"1958","RETRIEVAL_ID":1,"INDICATOR":"I","CHANGED_ON":"20181012","ZNGED_AT":"102708","ZGED_BY":"PT"}
{"MT":"500","FISC_YR":"1958","RETRIEVAL_ID":1,"INDICATOR":"I","CHANGED_ON":"20181013","ZNGED_AT":"102709","ZGED_BY":"PT"}

我需要编写代码以将上述内容转换为具有如下数据的csv文件:

MT,FISC,RETRIEVAL_ID,INDICATOR,CHANGED_ON,ZNGED_AT,ZGED_BY
500,1958,1,I,20181012,102708,PT  
500,1957,2,F,20181013,102709,AT

我对KAFKA完全陌生。我从下面的代码开始。

from kafka import KafkaConsumer
consumer = KafkaConsumer('my_topic')
for msg in consumer:
    print (msg)

1 个答案:

答案 0 :(得分:0)

您可以使用DictWriter功能:

import csv
import json

data = [{'MT': '500', 'FISC_YR': '1958', 'RETRIEVAL_ID': 1,'INDICATOR': 'I', 'CHANGED_ON': '20181012', 'ZNGED_AT': '102708', 'ZGED_BY': 'PT'},
        {'KT': '500', 'FISC_YEAR': '1958', 'RETRIEVAL_ID': 1,'INDICATOR': 'I', 'CHANGED_ON': '20181013', 'ZNGED_AT': '102709', 'ZGED_BY': 'PT'}]

data = json.loads((json.dumps(data).replace('KT', 'MT').replace('FISC_YR', 'FISC')))

with open('result.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=['MT', 'FISC', 'RETRIEVAL_ID', 'INDICATOR', 'CHANGED_ON', 'ZNGED_AT', 'ZGED_BY'])
    writer.writeheader()
    writer.writerows(data)

输出文件:

MT,FISC,RETRIEVAL_ID,INDICATOR,CHANGED_ON,ZNGED_AT,ZGED_BY
500,1958,1,I,20181012,102708,PT
500,1958,1,I,20181013,102709,PT