json转csv

import csv
import json
import re
import xlwt
class IO_rw(object):

def __init__(self):
#excel
self.f = xlwt.Workbook(encoding='utf-8')
self.sheet1 = self.f.add_sheet(u'sheet1', cell_overwrite_ok=True)

#csv
self.file_json = open("post_weibo_simple.json", "r+", encoding="utf-8")
self.csvfile = open("test.csv", "w")
self.writer = csv.writer(self.csvfile)
b = ['id','name','location',"url","commentCount","pDate","fansCount","description","likeCount","shareCount","friendCount",
"postCount"]
self.writer.writerow(b)

def process_item(self):

dict_json = json.loads(self.file_json.read())

for i_from in dict_json['data']:

shareCount = i_from['shareCount']

id = i_from['id']

commentCount = i_from['commentCount']

pDate = i_from['pDate']

dict_i = i_from['from']

friendCount = dict_i['friendCount']

name = dict_i['name']

likeCount = dict_i['likeCount']

description = dict_i['description']

url = dict_i['url']

postCount = dict_i['postCount']

fansCount = dict_i['fansCount']

loca = dict_i['extend']

location = loca['location']


a = [id,name,location,url,commentCount,pDate,fansCount,description,likeCount,shareCount,friendCount,postCount]
self.writer.writerow(a)


def close_spider(self):
self.file_json.close()
self.csvFile.close()

if __name__ == '__main__':
r = IO_rw()
r.process_item()

posted on 2018-11-08 14:25  袁佳佳  阅读(505)  评论(0编辑  收藏  举报

导航