zoukankan      html  css  js  c++  java
  • json转csv

    import re
    
    # csv格式
    # 'k1,k2,k3
    v1,v2,v3
    v4,v5,v6
    '
    
    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')) + '
    '
        
        # 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) + '
    '
    
        return csv
    
    def multi_prod_to_csv(data):
        csv = ''
    
        # head
        csv = csv + ','.join(data.get('fields')) + '
    '
    
        # 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) + '
    '
    
        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) + '
    '
    
        # 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) + '
    '
    
        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')))
  • 相关阅读:
    Webpack 2 视频教程 004
    Webpack 2 视频教程 003
    python dic字典排序
    python练习题7.1词频统计
    python文件读写的基础使用(计算总评成绩)
    python常用内置函数使用
    python练习题5.7列表去重(修正)
    python练习题5.2图的字典表示
    python练习题4.15换硬币(修正)
    python练习题4.7统计学生平均成绩与及格人数
  • 原文地址:https://www.cnblogs.com/jzm17173/p/5142610.html
Copyright © 2011-2022 走看看