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')))

json转csv的更多相关文章

  1. ActiveReports 报表控件V12新特性 -- 新增JSON和CSV导出

    ActiveReports是一款专注于 .NET 平台的报表控件,全面满足 HTML5 / WinForms / ASP.NET / ASP.NET MVC / WPF 等平台下报表设计和开发工作需求 ...

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

    #tablib把数据导出为Excel.JSON.CSV等格式的Py库 #python 3 import tablib #定义列标题 headers = ('1列', '2列', '3列', '4列', ...

  3. python将json转csv

    现有一个需求要将json转成excel,使用python将其转为csv格式,使用excel打开即可. import json import csv import codecs f = open('te ...

  4. solr6.6 导入 文本(txt/json/xml/csv)文件

    参照:solr6.6 导入 pdf文件 重点就是三个配置文件 1.建立的data-config.xml 内容如下: <dataConfig> <dataSource name=&qu ...

  5. solr File Upload "Unsupported ContentType: application/vnd.ms-excel Not in: [application/xml, application/csv, application/json, text/json, text/csv, text/xml, application/javabin]",

    今天在用solr管理界面导入文件时报错:"Unsupported ContentType: application/vnd.ms-excel  Not in: [application/xm ...

  6. 最简单的JS实现json转csv

    工作久了,总会遇到各种各样的数据处理工作,比如同步数据,初始化一些数据,目前比较流行的交互数据格式就是JSON,可是服务器中得到的JSON数据如果提供给业务人员看的话可能会非常不方便,这时候,转成CS ...

  7. JSON导出CSV中文乱码解决方案

    前言 以往datagrid导出数据全部在后台搞定,现在就想换中思路去解决,正常情况下使用easyui datagrid ajax获取数据源时都是json格式,那么此时需要导出数据时只要把该数据源扔出来 ...

  8. Mongodb 导出json 和csv 格式数据

    导出到json: $ mongoexport.exe  -d TestDB -c TestCollection -o  ./test.json 导出到csv: If you want to outpu ...

  9. json与csv的基础用与法

    json库是处理json格式的python标准库 有两个过程: 编码(encoding):将python数据类型转换为json格式的过程 解码(decoding):从json格式中解析数据得到的pyt ...

  10. 转载:python生成以及打开json、csv和txt文件

    原文地址:https://blog.csdn.net/weixin_42555131/article/details/82012642 生成txt文件: mesg = "hello worl ...

随机推荐

  1. Codeforces Round #358 (Div. 2) D. Alyona and Strings 字符串dp

    题目链接: 题目 D. Alyona and Strings time limit per test2 seconds memory limit per test256 megabytes input ...

  2. 新建标准mavenWeb工程以及Maven的web应用标准目录结构建议

    到现在为止,使用Maven结构的Web工程越来越多,因此在此介绍一下通过Maven来构建项目的相关知识.     文档主要分为两部分:       1.如何通过maven来构建多模块的web项目    ...

  3. 编写一个递归函数,输出vector对象的内容

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  4. NYOJ-469 擅长排列的小明 II AC 分类: NYOJ 2014-01-02 22:19 159人阅读 评论(0) 收藏

    最初的第一印象是和组合数一个性质的题目.所以用了回溯法,结果,你懂的... #include<stdio.h> #include<math.h> void dfs(int n, ...

  5. PE文件结构详解(二)可执行文件头

    在PE文件结构详解(一)基本概念里,解释了一些PE文件的一些基本概念,从这篇开始,将详细讲解PE文件中的重要结构. 了解一个文件的格式,最应该首先了解的就是这个文件的文件头的含义,因为几乎所有的文件格 ...

  6. Lua print on the same line

    In Pascal, I have write and writeln. Apparently Lua's print is similar to writeln of Pascal. Do we h ...

  7. HDU 3461 Code Lock(并查集,合并区间,思路太难想了啊)

    完全没思路,题目也没看懂,直接参考大牛们的解法. http://www.myexception.cn/program/723825.html 题意是说有N个字母组成的密码锁,如[wersdfj],每一 ...

  8. [LeetCode]Link List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  9. hdu 1536/1944 / POJ 2960 / ZOJ 3084 S-Nim 博弈论

    简单的SG函数应用!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #inclu ...

  10. Spark安装部署

    原创文章,转载请注明: 转载自www.cnblogs.com/tovin/p/3820979.html 一.系统环境配置 参照http://www.cnblogs.com/tovin/p/381890 ...