json转csv

import re

# csv格式
# ‘k1,k2,k3\nv1,v2,v3\nv4,v5,v6\n‘

market_list_data = {
    "data": [
        {
            "finance_mic": "SS",
            "finance_name": "上海证券交易所"
        },
        {
            "finance_mic": "SZ",
            "finance_name": "深圳证券交易所"
        },
        {
            "finance_mic": "CCFX",
            "finance_name": "中国金融期货交易所"
        }
    ]
}

tick_data = {
  "data": {
    "tick": {
      "fields": [
        "business_time",
        "hq_px",
        "business_amount",
        "business_balance",
        "business_count",
        "business_direction"
      ],
      "600570.SS": [
        [
          20150907092501,
          41.58,
          13000378,
          540555717,
          0,
          0
        ],
        [
          20150907093000,
          42.1,
          148300,
          6254573,
          0,
          1
        ]
      ]
    }
  }
}

kline_data = {
  "data": {
    "candle": {
      "fields": [
        "min_time",
        "open_px",
        "high_px",
        "low_px",
        "close_px"
      ],
      "600570.SS": [
        [
          201501061401,
          54890,
          54890,
          54600,
          54600
        ],
        [
          201501061402,
          54610,
          54610,
          54400,
          54400
        ]
      ]
    }
  }
}

trend_data = {
  "data": {
    "trend": {
      "fields": [
        "min_time",
        "last_px",
        "avg_px",
        "business_amount"
      ],
      "600570.SS": [
        [
          201501090930,
          54.98,
          54.98,
          28327
        ],
        [
          201501090931,
          54.63,
          54.829486,
          49700
        ]
      ]
    }
  }
}

real_data = {
  "data": {
    "snapshot": {
      "fields": [
        "data_timestamp",
        "shares_per_hand",
        "open_px",
        "high_px",
        "low_px",
        "last_px",
        "business_amount",
        "business_balance",
        "offer_grp",
        "bid_grp"
      ],
      "600570.SS": [
        150305133,
        100,
        53980,
        56000,
        52510,
        54950,
        14465811,
        788995643,
        "54850,9887,0,54860,1500,0,54900,13300,0,54950,10000,0,54990,800,0,",
        "54820,8000,0,54810,2100,0,54800,202900,0,54770,100,0,54720,1200,0,"
      ],
      "000001.SZ": [
        153229327,
        100,
        15560,
        15830,
        15300,
        15480,
        170012067,
        2634796408,
        "15490,93700,0,15500,260609,0,15510,69996,0,15520,87008,0,15530,71400,0,",
        "15480,438292,0,15470,149000,0,15460,411400,0,15450,414573,0,15440,303733,0,"
      ]
    }
  }
}

trend5day_data = {
  "data": {
    "trend": {
      "fields": [
        "min_time",
        "last_px",
        "avg_px",
        "business_amount",
        "business_balance"
      ],
      "600570.SS": [
        [
          201504170931,
          124.07,
          124.83,
          387955,
          48426658
        ],
        [
          201504170932,
          123.01,
          124.59,
          572900,
          71378859
        ],
        [
          201504170933,
          121.89,
          123.94,
          834100,
          103378268
        ],
        [
          201504170934,
          122.44,
          123.74,
          941239,
          116465390
        ]
      ]
    }
  }
}

def one_prod_to_csv(data):
    csv = ‘‘

    # head
    csv = csv + ‘,‘.join(data.get(‘fields‘)) + ‘\n‘

    # body
    for item in data.keys():
        if not item is ‘fields‘:
            body_key = item

    body_data = data.get(body_key)

    for item in body_data:
        new_item = []
        for v in item:
            new_item.append(re.sub(r‘,‘, ‘|‘, str(v)))
        csv = csv + ‘,‘.join(new_item) + ‘\n‘

    return csv

def multi_prod_to_csv(data):
    csv = ‘‘

    # head
    csv = csv + ‘,‘.join(data.get(‘fields‘)) + ‘\n‘

    # body
    for k,v in data.items():
        if not k is ‘fields‘:
            new_v = []
            for item in v:
                new_v.append(re.sub(r‘,‘, ‘|‘, str(item)))
            csv = csv + ‘,‘.join(new_v) + ‘\n‘

    return csv

def dict_to_csv(data):
    csv = ‘‘

    head_data = []
    body_data = []

    for item in data[0].keys():
        head_data.append(item)

    # head
    csv = csv + ‘,‘.join(head_data) + ‘\n‘

    # body
    for item in data:
        new_item = []
        for k,v in item.items():
            new_item.append(v)
        body_data.append(new_item)

    for item in body_data:
        csv = csv + ‘,‘.join(item) + ‘\n‘

    return csv

def market_list_to_csv(data):
    return dict_to_csv(data)

def tick_to_csv(data):
    return one_prod_to_csv(data)

def kline_to_csv(data):
    return one_prod_to_csv(data)

def trend5day_to_csv(data):
    return one_prod_to_csv(data)

def trend_to_csv(data):
    return one_prod_to_csv(data)

def real_to_csv(data):
    return multi_prod_to_csv(data)

print(market_list_to_csv(market_list_data.get(‘data‘)))
print(tick_to_csv(tick_data.get(‘data‘).get(‘tick‘)))
print(kline_to_csv(kline_data.get(‘data‘).get(‘candle‘)))
print(trend5day_to_csv(trend5day_data.get(‘data‘).get(‘trend‘)))
print(trend_to_csv(trend_data.get(‘data‘).get(‘trend‘)))
print(real_to_csv(real_data.get(‘data‘).get(‘snapshot‘)))
时间: 2024-10-12 04:42:07

json转csv的相关文章

JSON与CSV的转换

思路: 先把多个JSON元素转换成数组,然后使用python的csv和json模块来处理. 范例格式: [ {     "description": null,     "image": {       "image_size": 20,       "os_family": "centos",       "platform": "linux",       &qu

tablib把数据导出为Excel、JSON、CSV等格式的Py库(写入数据并导出exl)

#tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', '5列') #需写入的数据,按照一行一行的输入 #元组数据的个数必须和列数一致 data = [('23','23','34','23','34'),('sadf','23','sdf','23','fsad')] #写入数据 mylist = tablib.Dataset(*data, head

python将json转csv

现有一个需求要将json转成excel,使用python将其转为csv格式,使用excel打开即可. import json import csv import codecs f = open('test.json') data = json.load(f) #print(data) f.close() f = codecs.open('test.csv', 'w', 'utf_8_sig')#解决写入csv时中文乱码 writer=csv.writer(f); for item in data

Python-爬虫-(Json和Csv)文件存储

用解析器解析出的数据后,可以通过TXT.JSON.CSV等文件形式进行存储: 1.TXT形式此处略: 2.JSON文件存储 json即js 对象标记,是一种数据格式: json格式: json对象:{"username":"ADMIN","pwd":"xxxx","address":"北京"} json数组:[{"name":"smith",&qu

Json转csv的网站json-csv.com如何免费|不限制大小

https://json-csv.com/是可以将json转换为csv文件的网站,今天我试了一下效果不错,但是文件大一点就需要收费. 怎么办呢?抱着试一试的心态我按下了F12,点开NetWork,发现实际的下载链接长这样: https://json-csv.com/conversion/download?id=2ef8d4850f1a40618bbb025fbe28a884&delimeter=0&filename=poems-single&timeStamp=10623706&a

【BOOK】数据存储—文件存储(TXT、JSON、CSV)

数据存储 文本文件—TXT.JSON.CSV 关系型数据库—MySQL.SQLite.Oracle.SQL Server.DB2 非关系型数据库—MongoDB.Redis   文件打开 open(),第二个参数设置文件打开方式 ※      r:只读,文件指针在文件开头 ※      rb:二进制只读,文件指针在文件开头 ※      r+:读写方式,文件指针在文件开头 ※      w:写入,如果文件已存在,则覆盖:若文件不存在,则新建 ※      wb:二进制写入,如果文件已存在,则覆盖

Mongodb 导出json 和csv 格式数据

导出到json: $ mongoexport.exe  -d TestDB -c TestCollection -o  ./test.json 导出到csv: If you want to output CSV, you have to specify the fields in the order you want them. $ mongoexport.exe  --csv -f _id,f1,f2,f3,f4,f5,f6,f7 -d TestDB -c TestCollection -o

(完整)爬取数据存储之TXT、JSON、CSV存储

一.文件存储 1. TXT文本存储 例:知乎发现页面,获得数据存成TXT文本 import requests from pyquery import PyQuery as pq url="https://www.zhihu.com/explore" headers={'User-Agent':'ozilla/5.0 (iPhone; CPU iPhone OS 11_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mo

Java 对不同类型的数据文件的读写操作整合器[JSON,XML,CSV]-[经过设计模式改造](2020年寒假小目标03)

日期:2020.01.16 博客期:125 星期四 我想说想要构造这样一个通用文件读写器确实不容易,嗯~以后会添加更多的文件类型,先来熟悉一下文件内容样式: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beangroup> 3 <javabean> 4 <data name='code'>A001</data> 5 <data name='name'>

js json转csv

$(document).ready(function() { "use strict"; var mo = { init: function() { $('.download').click(function() { var data = $('#txt').val(); if (data === '') { return; } mo.JSONToCSVConvertor(data, true); }); }, JSONToCSVConvertor: function(JSONData