Home > Net >  python live data in excel
python live data in excel

Time:09-10

this code print live data but i want output in excel with only price change like this--->

symbol price
DOGEUSDT 0.0633400
BTCUSDT 21013.040
LTCUSDT 60.9000

code---->>> `

import pandas as pd
import requests
import json

ltp_data= []

crypto = {"BTCUSDT",  "LTCUSDT", "DOGEUSDT"}
def live_ltp():
    responses = []
    for i in crypto:
        key = "https://api.binance.com/api/v3/ticker/price?symbol="
        url = key i
        response = requests.get(url)
        Ltp = response.json()
        ltp_data.append(Ltp)
        responses.append(Ltp)
    return responses
while True:
    print(str(live_ltp()))

`

CodePudding user response:

try:

while True:
    stats = pd.DataFrame(live_ltp())
    with pd.ExcelWriter('Output.xlsx') as excel_writer:
        stats.to_excel(excel_writer, sheet_name='live_ltp', index=False)

maybe have a time.sleep at the end so it isn't spamming the API too.

CodePudding user response:

If I understood well, you want to update an excel file using the while loop. If you put your request in a dataframe, it is easily done using openpyxl. The following works well, as long as the file is close.

import openpyxl

try:
    with pd.ExcelWriter(path, engine = 'openpyxl', mode = 'a', if_sheet_exists = 'overlay') as writer:
        YourOutputDataFrame.to_excel(writer, sheet_name = sheet_name, index = False) #Write output in a specific sheet (by name or index)
        writer.book.close() #Close (and save) Excel file
except:
    pass

Hope this helps. Do not hesitate to ask more about formatting, etc. if needed.

  • Related