Save to a new JSON file then append every other record to it

Viewed 28

I wrote the code below to extract some information about a list of stock symbols and save them to a single JSON file. My issue is that the resulting JSON file contains only one record. Every time the process fetches information about a new symbol it deletes the old one from the JSON file and then writes the new record. What am I doing wrong?!

One more side thing, I also want to add "tqdm" progress bar indicator to this process, how can I do that?

def get_stats(symbol):
    info = yf.Tickers(symbol).tickers[symbol].info
    stocks_info = {}
    info = {'symbol': symbol, 'longName': info['longName'], 'country': info['country'], 'sector': info['sector'],
            'industry': info['industry'], 'marketCap': info['marketCap'], 'currentPrice': info['currentPrice'],
            'quoteType': info['quoteType'], 'market': info['market']}
    stocks_info[symbol] = info
    json_object = json.dumps(stocks_info)
    with open("../tickers_data/stocks_info.json", "w") as outfile:
        outfile.write(json_object)


with ThreadPoolExecutor() as executor:
      executor.map(get_stats, sav_set)

Sample result:

{"AKICU": {"symbol": "AKICU", "longName": "Sports Ventures Acquisition Corp.", "country": "United States", "sector": "Financial Services", "industry": "Shell Companies", "marketCap": null, "currentPrice": 9.91, "quoteType": "EQUITY", "market": "us_market"}}

only one record!

0 Answers
Related