简体   繁体   中英

Converting a list to a CSV file with python

I am trying to Convert a list returned by an API to a defined table with rows and columns using pandas.
but for some reason, it has been returning an error. Below is my code and error as well.

import requests
import pprint
import json
import pandas as pd


url = "https://yahoo-finance15.p.rapidapi.com/api/yahoo/ga/topgainers"

querystring = {"start":"0"}

headers = {
    'x-rapidapi-host': "yahoo-finance15.p.rapidapi.com",
    'x-rapidapi-key': "9efd0f3e52mshd859f5daf34a429p11cb2ajsn2b0e421d681e"
    }

response = requests.request("GET", url, headers=headers, params=querystring)
data = response.json()

#print(response.text)


def new_stock(data):
    new_market = []

    for item in data ['quotes']:
        new_name = item.get ('longName')
        new_price = item.get ('regularMarketPrice')
        res_price = (f'{new_price} Dollars')
        cap =item.get('marketCap')
        new_market.append((new_name, res_price, cap))

    return new_market

value = new_stock(data)



def new_list(share_value):
    new = []

    for items in share_value:
        JSONContent = value
        if 'error' not in JSONContent:
            new.append([JSONContent['longName'], JSONContent['regularMarketPrice'], JSONContent['marketCap']])

    return new

dataset = pd.read_csv(new_stock(value))
df = dataset.DataFrame
base = dataset.sample(3)

I keep getting this error in return:

   for item in data ['quotes']:
TypeError: list indices must be integers or slices, not str

The problem is at dataset = pd.read_csv(new_stock(value)) . At this point, value is a list of tuples and not a dictionary as you expect.

I think that you by mistake trying to process it again with new_stock function instead of using somehow the other function you defined ( new_list ) and is not used in your code.

Why don't you use json_normalize instead?

response = requests.request("GET", url, headers=headers, params=querystring)
data = response.json()

df = pd.json_normalize(data, ['quotes'])
print(df.head())

  language region quoteType  triggerable currency  ...  earningsTimestamp  earningsTimestampStart  earningsTimestampEnd  epsForward  forwardPE
0       US   ENUS    EQUITY        False      USD  ...                NaN                     NaN                   NaN         NaN        NaN
1       US   ENUS    EQUITY        False      USD  ...       1.584031e+09            1.584031e+09          1.584031e+09       -0.80 -18.237500
2       US   ENUS    EQUITY        False      USD  ...       1.588696e+09            1.588696e+09          1.588696e+09       -7.07 -17.314003
3       US   ENUS    EQUITY        False      USD  ...       1.587672e+09            1.595635e+09          1.596067e+09        0.95  52.452633
4       US   ENUS    EQUITY        False      USD  ...       1.587673e+09            1.595522e+09          1.595867e+09        1.72  26.040697

[5 rows x 71 columns]

You can also save it to csv:

df.to_csv('yahoo.csv')

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