简体   繁体   中英

Adding scrip to export python result from json to excel or csv file

I am new to the Python world so apologies. I am looking into a set of time series I have downloaded from an API. These data have been printed as JSON file (as seen in the below code). However, how can I convert this data from JSON to EXCEL or CSV?

Below is what I used:

import requests
payload = "{\"startDate\": 20190930, \"endDate\": 20191018, \"tags\":[\"PAYMENT.REIMBURSEMENT.1Y.BLOOMBERG\"]}"
headers = {
'content-type': "application/json",
'accept': "application/json",
'authorization': "Bearer AAIkOWNmNGVjMzctYTg5MC00YjhiLWE4MTEtNmE4ZDUzMzU1OWVlGUDsir"
}
proxyDict = {"http": "http://webproxy.payment.nsroot.net:8080/", "https": "http://payment.nsroot.net:8080/"}
r = requests.post(url="https://payment.com/data?client_id=9cf4ec37-a890-4b8b-a811-6a8d533559ee", data=payload, headers=headers, proxies=proxyDict)
print(r.json())

Below is the result that the above code is providing.

{'frequency': 'DAILY', 'body': {'PAYMENT.REIMBURSEMENT.1Y.BLOOMBERG': {'x': [20190930, 20191001, 20191002, 20191003, 20191004, 20191007, 20191008, 20191009, 20191010, 20191011, 20191014, 20191015, 20191016, 20191017, 20191018], 'c':[ [60.0392, 60.0391, 60.0391, 60.0391, 60.0391, 60.0391, 60.0391, 60.0391, 60.0391, 60.0391, 60.0391, 60.039, 60.039, 60.039, 60.039]][1], 'type': 'SERIES'}}, 'status': 'OK'}

Can you let me know how to convert that to csv file? Where Column A is the Date in (yyyymmdd) and Column B are the Values like the below >

Dates Values 20190930 60.0392... ... 20191018 60.039

Thank you.

result = list(zip(test['body']['PAYMENT.REIMBURSEMENT.1Y.BLOOMBERG']['x'], test['body']['PAYMENT.REIMBURSEMENT.1Y.BLOOMBERG']['c']))

import csv
with open('PAYMENT.REIMBURSEMENT.1Y.BLOOMBERG', 'w', newline='') as csvfile:
    fieldnames = ['Date', 'values']
    writer = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    writer.writerow(fieldnames)
    for row in result:
        writer.writerow(row)

if you need to convert to excel with sheet name following is the code.

import pandas as pd

df = pd.DataFrame(result, columns=['Date', 'values'])

df.to_excel('file.xlsx', sheet_name='PAYMENT.REIMBURSEMENT.1Y.BLOOMBERG', index=False)

First create a path to you desktop, and put it in a constant (conventionally noted in UPPER_CASE):

import pathlib

DESKTOP_PATH = pathlib.Path('/Users/user_name/Desktop')

Iterate over the key and values in you json's body, as per your request, the key is used as the file name, which is why it is held in a variable by that name, and the values are the dictionary holding the csv data:

for file_name, data in r.json()['body']:
    csv_rows = '\n'.join(','.join(values) for values in zip(*data.values()))
    (DESKTOP_PATH / filename).write_text(f'{",".join(data.keys())}\n{csv_rows}')

Inside the for loop, we build the csv corresponding to each file: zip(*data.values()) converts the values list, which holds a list of lists in the form of [column_a_values, column_b_values, ...] , to an iterator which combines all the elements on the same row. ie list(zip(*[[1, 2, 3], [4, 5, 6], [7, 8, 9]])) equals [[1, 4, 7], [2, 5, 8], [3, 6, 9]] .

We then join all the values in a row by with a comma by using ','.join(values) and join the rows with new lines between them by using '\n'.join(...) .

Finally, we write all that text in one go using write_text on a concatenation of the desktop path and the filename given by DESKTOP_PATH / filename , and the final text is given by f'{",".join(data.keys())}\n{csv_rows}' which first outputs the column names, and then the rows we have calculated above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM