I made this code where the purpose its to retrieve data from an API from a certain number of stock tickers
import requests
import json
import csv
tickers = open("ticker_list.txt","r")
for ticker in tickers:
ticker = ticker.strip()
url = "https://xxx/wallstreetbets/"+ticker
headers = {'accept': 'application'}
r = requests.get(url, headers=headers)
data = r.json()
json_string = json.dumps(data, indent=1)
for tick in data:
print(tick ['Date'],tick['Mentions'])
This returns for every ticker an output like this
2018-08-10 1
2018-08-28 1
2018-09-07 1
2018-10-09 1
2018-10-18 1
2018-11-28 1
2019-01-04 1
2019-01-08 1
2019-01-10 1
2019-01-16 5
2019-01-23 1
2019-01-24 1
Where we have the date on the left, and the number of mentions on the right after a space.
What I want to do now is to store this result into a csv file where I have date, mentions in column so that when i open the csv file with excel I have:
2018-08-10,1
2018-08-28,1
2018-09-07,1
...
Thanks in advance for the answers!