Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将json转换为csv_Python_Json_Python 3.x - Fatal编程技术网

Python 如何将json转换为csv

Python 如何将json转换为csv,python,json,python-3.x,Python,Json,Python 3.x,我正在尝试将下面的json输出转换为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:1

我正在尝试将下面的json输出转换为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]]}}
我尝试使用以下代码,但Output不应为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)
所需输出格式

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

在这种情况下,你可以考虑使用熊猫,因为看起来你有一个矩形数据:

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

使用pandas提供的两个答案对于任务来说是一种过分的技巧,并且没有说明所请求的时间和日期格式。 我将使用已经包含的csv软件包:

输出:

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
...
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
...