dimanche 20 mai 2018

Python Loop Understanding

Im self learning Python , and i've been stuck on this loop problem for a while. I wrote a script to extract Data every hour about stock prices. But for each hour of data I have manually created new variables to hold my new data.

  • Is there a loop that will do this for me without having to create a new line of variables for each run?
  • and can that loop create a text file that has a differing name? ( ex: Txtfile1.txt , Txtfile2.txt)

code ex attached below

Thanks

 R = requests.get("https://api.website.com/v1/....)
 data = R.json()

for x in data:
       ticker = x['symbol']
       cost = x['price_usd']

print (ticker + "\t:" + cost)
with open('Marketcap.txt', 'w') as outfile:
    json.dump(data, outfile, indent=2)

time.sleep(3600)


R2 = requests.get("https://api.website.com/v1/....)
data2 = R2.json()

for y in data2:

        ticker2 = y['symbol']
        cost2 = y['price_usd']

print(ticker2  +":\t" cost2)

with open('Marketcap2.txt', 'w') as outfile2:
    json.dump(data2, outfile2, indent=2)

time.sleep(3600)

Aucun commentaire:

Enregistrer un commentaire