簡體   English   中英

如何將json轉換為csv

[英]How to convert json into csv

我正在嘗試將以下 json output 轉換為 csv。

{u'status': u'success', u'data': {u'candles': [[u'2020-05-15T09:15:00+0530', 9163.95, 9165.05, 9090.55, 9115.9, 440475, 7516350], [u'2020-05-15T09:17:00+0530', 9116.25, 9122.5, 9090, 9090.05, 266925, 7550850], [u'2020-05-15T09:19:00+0530', 9090.05, 9095, 9076.6, 9095, 290325, 7609125], [u'2020-05-15T09:21:00+0530', 9094.8, 9095.15, 9082.35, 9092.35, 179925, 7609125], [u'2020-05-15T09:23:00+0530', 9093, 9102.2, 9085, 9097.65, 132450, 7687050], [u'2020-05-15T09:25:00+0530', 9099.7, 9114.8, 9098.3, 9106.85, 165375, 7681425], [u'2020-05-15T09:27:00+0530', 9110.5, 9129.8, 9108.95, 9129.1, 205800, 7681425], [u'2020-05-15T09:29:00+0530', 9127.45, 9142.5, 9119.35, 9138.95, 189525, 7684050], [u'2020-05-15T09:31:00+0530', 9139.95, 9143.15, 9124, 9127, 150975, 7665525], [u'2020-05-15T09:33:00+0530', 9126.9, 9140, 9124, 9138, 101400, 7665525]]}}

我嘗試使用以下代碼但輸出並不期望為 csv

datedata = json_obj3['data']['candles']
encodedUnicode = json.dumps(datedata, ensure_ascii=False).encode('utf-8')
print type(encodedUnicode)

final = encodedUnicode.replace("], [","\n").replace("T"," ").replace("+0530",".000").replace('"','').replace('[[','').replace(']]','').replace('-','.')
print type(final)

需要 output 格式

time,Open,High,Low,Close,Volume
02.09.2019 00:00:00.000,1529.405,1529.505,1528.895,1529.005,71499.9997
02.09.2019 00:03:00.000,1528.932,1529.262,1528.905,1529.185,72030.0004
02.09.2019 00:06:00.000,1529.125,1529.405,1529.015,1529.332,33129.9989

使用 pandas 提供的兩個答案對任務來說是一種矯枉過正,也沒有解決所請求的時間和日期格式。 我將 go 與已經包含的csv package:

import csv
from datetime import datetime

data = json_obj3['data']['candles']

with open('out.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["time", "Open", "High", "Low", "Close", "Volume"])
    for row in data:
        dt = datetime.strptime(row[0], "%Y-%m-%dT%H:%M:%S%z")
        formatted_dt = dt.strftime("%d.%m.%Y %H:%M:%S.000")
        writer.writerow([formatted_dt] + row[1:])

Output:

time,Open,High,Low,Close,Volume
15.05.2020 09:15:00.000,9163.95,9165.05,9090.55,9115.9,440475,7516350
15.05.2020 09:17:00.000,9116.25,9122.5,9090,9090.05,266925,7550850
...

在這種情況下,您可以考慮使用 pandas,因為看起來您有一個矩形數據:

import pandas as pd
df = pd.DataFrame(pd.DataFrame(json_obj3['data']).candles.tolist()).\
              rename({0:'time',1:'open',2:'high',3:'low',4:'close',5:'volume'},axis=1)

因為你想轉換為csv你可以這樣做:

df.to_csv('path_to_your_file.csv', index = False)

結果應類似於:

print(df.to_csv(index = False)

time,open,high,low,close,volume,6
2020-05-15T09:15:00+0530,9163.95,9165.05,9090.55,9115.9,440475,7516350
2020-05-15T09:17:00+0530,9116.25,9122.5,9090.0,9090.05,266925,7550850
2020-05-15T09:19:00+0530,9090.05,9095.0,9076.6,9095.0,290325,7609125
2020-05-15T09:21:00+0530,9094.8,9095.15,9082.35,9092.35,179925,7609125
2020-05-15T09:23:00+0530,9093.0,9102.2,9085.0,9097.65,132450,7687050
2020-05-15T09:25:00+0530,9099.7,9114.8,9098.3,9106.85,165375,7681425
2020-05-15T09:27:00+0530,9110.5,9129.8,9108.95,9129.1,205800,7681425
2020-05-15T09:29:00+0530,9127.45,9142.5,9119.35,9138.95,189525,7684050
2020-05-15T09:31:00+0530,9139.95,9143.15,9124.0,9127.0,150975,7665525
2020-05-15T09:33:00+0530,9126.9,9140.0,9124.0,9138.0,101400,7665525

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM