利用pandas生成csv文件

# -*- coding:UTF-8 -*-
import json
from collections import OrderedDict

with open('dns_status.json','r') as f:
    data = json.load(f)

import pandas as pd
from datetime import datetime
def datelist(beginDate, endDate):
    # beginDate, endDate是形如‘20160601’的字符串或datetime格式
    date_l=[datetime.strftime(x,'%Y-%m') for x in list(pd.date_range(start=beginDate, end=endDate))]
    date_set=list(set(date_l))
    date_set.sort(key=date_l.index)
    return date_set

dates = datelist('20090615','20171030')

dd = OrderedDict()
for d in data:
    for s_date in dates:
        if s_date in d['date']:
            dd[s_date] = d
            d['date'] = s_date

l = []
for ii in dd:
    l.append(dd[ii])# dd[ii]是字典
df = pd.DataFrame() #写入csv
result = df.append(l, ignore_index=True)
result.to_csv('test1.csv')

 

posted @ 2017-11-01 15:28  明天OoO你好  阅读(11500)  评论(0编辑  收藏  举报