amit_python
amit_python

Reputation: 9

csv to dictionary from urllib

With urllib i go to address that downloads a csv. I need to get the csv back as a Dictionary to the pycharm (and not as excel file).

dictionary keys are (as in the file) - Date,Open,High,Low,Close,Adj Close,Volume

I would appreciate your assistance.

from urllib.request import urlopen


with urlopen("https://query1.finance.yahoo.com/v7/finance/download/aapl?period1=1640988000&period2=1657314000&interval=1d&events=history&includeAdjustedClose=true&volume") as response:
    html = response.readline()


print(html)

Upvotes: 0

Views: 90

Answers (1)

y051
y051

Reputation: 184

import csv
import urllib2

url = 'https://query1.finance.yahoo.com/v7/finance/download/aapl?period1=1640988000&period2=1657314000&interval=1d&events=history&includeAdjustedClose=true&volume'
response = urllib2.urlopen(url)

reader = csv.reader(response)
row1 = next(reader) 
dic = {}
for lines in reader:
    for i in range(len(row1)):
        if row1[i] not in dic:
            dic[row1[i]] = []
        dic[row1[i]].append(lines[i])
print(dic)  

Im not sure, but I believe this is what something you are expecting, if not please feel free to comment. Thanks

Upvotes: 0

Related Questions