Skip to content Skip to sidebar Skip to footer

Save Json File Contents To Csv File In Python/pandas

How to get the 'data' information into a csv table as shown at the end (and also, the right 'headers' so that the source server doesn't throw me off thinking I am scraping)? The c

Solution 1:

import requests
import pandas as pd

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'
}


defmain(url):
    r = requests.get(url, headers=headers).json()
    x = []
    for item in r['data']:
        df = pd.DataFrame.from_dict([item])
        x.append(df)
    new = pd.concat(x, ignore_index=True)
    print(new)
    new.to_csv("Data.csv")


main("https://www1.nseindia.com/live_market/dynaContent/live_watch/stock_watch/foSecStockWatch.json")

Output: view online

enter image description here

Post a Comment for "Save Json File Contents To Csv File In Python/pandas"