如何使用python将csv文件转换为JSON文件

时间:2015-12-03 12:48:38

标签: python json csv

我需要将csv文件转换为JSON对象(最好使用Python)

输入文件是:

**Jun 28 23:59:56 MUM-NM-FG-FW/MUM-NM-FG-FW date=2014-06-28 time=23:59:59 devname=MUM-NM-FG-FW-PRI device_id=FG300C3911600511 log_id=0038000004 type=traffic subtype=other pri=notice vd=Sify-VDOM src=XXX.XXX.XXX.XXX src_port=60276 src_int="port5" dst=XXX.XXX.XXX.XXX 


Jun 28 23:59:56 MUM-NM-FG-FW/MUM-NM-FG-FW date=2014-06-28 time=23:59:59 devname=MUM-NM-FG-FW-PRI device_id=FG300C3911600511 log_id=0038000004 type=traffic subtype=other pri=notice vd=Sify-VDOM src=XXX.XXX.XXX.XXX  src_port=60277 src_int="port5" dst=XXX.XXX.XXX.XXX** 

结果如下:

{"date": "2014-06-28","time": "22:59:59","devname": "MUM-NM-FG-FW-PRI","device_id": "FG300C3911600511","log_id": "0038000004","type": "traffic","subtype": "other","pri": "notice","vd": "Sify-VDOM","src": "XXX.XXX.XXX.XXX","src_port": "60276","src_int": "port5","dst": "XXX.XXX.XXX.XXX" }

{"date": "2014-06-28","time": "22:59:59","devname": "MUM-NM-FG-FW-PRI","device_id": "FG300C3911600511","log_id": "0038000004","type": "traffic","subtype": "other","pri": "notice","vd": "Sify-VDOM","src": "XXX.XXX.XXX.XXX","src_port": "60277","src_int": "port5","dst": "XXX.XXX.XXX.XXX" }

请帮忙。

1 个答案:

答案 0 :(得分:0)

您可以在python中使用csv和json模块。 下面是解释如何操作的链接: http://www.idiotinside.com/2015/09/18/csv-json-pretty-print-python/