[Python] Trying to parse info from coinmarketcap API [duplicate]

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on some code to organize the data I receive from the API.

    It comes in with keys and values, but I haven't found a way to make a neat CSV with columns for keys and rows for values.

    When the data comes in via API, it's stored in a txt file.

    I take that txt file and sort it by replacing a character at end of line with \n.

    # Read in the file
    with open('latest_out.txt', 'r') as file :
    filedata = file.read()
    # Replace the target string
    filedata = filedata.replace('}}},', '\n')
    # Write the file out again
    with open('latest_out_sorted.txt', 'w') as file:
    file.write(filedata)


    This gives me a section with each CryptoCurrency.

    But I need to write this to a CSV with columns and values instead.

    Example of the text:


    {'id': 1, 'name': 'Bitcoin', 'symbol': 'BTC', 'slug': 'bitcoin', 'num_market_pairs': 9716, 'date_added': '2013-04-28T00:00:00.000Z', 'tags': ['mineable', 'pow', 'sha-256', 'store-of-value', 'state-channels', 'coinbase-ventures-portfolio', 'three-arrows-capital-portfolio', 'polychain-capital-portfolio'], 'max_supply': 21000000, 'circulating_supply': 18636068, 'total_supply': 18636068, 'platform': None, 'cmc_rank': 1, 'last_updated': '2021-02-23T02:28:02.000Z', 'quote': {'CAD': {'price': 65576.2472947517, 'volume_24h': 112161256452.41504, 'percent_change_1h': -2.63752472, 'percent_change_24h': -8.8113568, 'percent_change_7d': 6.37461057, 'percent_change_30d': 62.70380758, 'market_cap': 1222083403769.8086, 'last_updated': '2021-02-23T02:28:05.000Z'
    {'id': 1027, 'name': 'Ethereum', 'symbol': 'ETH', 'slug': 'ethereum', 'num_market_pairs': 5997, 'date_added': '2015-08-07T00:00:00.000Z', 'tags': ['mineable', 'pow', 'smart-contracts', 'coinbase-ventures-portfolio', 'three-arrows-capital-portfolio', 'polychain-capital-portfolio'], 'max_supply': None, 'circulating_supply': 114783223.6865, 'total_supply': 114783223.6865, 'platform': None, 'cmc_rank': 2, 'last_updated': '2021-02-23T02:28:02.000Z', 'quote': {'CAD': {'price': 2147.126992405774, 'volume_24h': 52728131634.694145, 'percent_change_1h': -3.49137429, 'percent_change_24h': -11.16189412, 'percent_change_7d': -6.56833787, 'percent_change_30d': 37.20883417, 'market_cap': 246454157852.63397, 'last_updated': '2021-02-23T02:28:05.000Z'


    How to read the data from CSV directly to a webpage?

    Continue reading...

Compartilhe esta Página