zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    汉字转拼音
    多数组求笛卡尔积
    curl post参数,接口接收不到数据问题
    判断IMEI或MEID是否合法
    javascript 可控速度的上下拉菜单
    去掉android点击事件产生的半透明蓝色背景
    go.js是什么
    jQuery效果——动画
    jQuery选择器
    vue全局组件局部组件的使用
  • 原文地址:https://www.cnblogs.com/yuanjia8888/p/9928844.html
Copyright © 2011-2022 走看看