Question

I'm attempting to pull data from Yahoo finance in the form of a .csv, then turn columns 1 and 5 into lists in Python. The portion of the code that turns the columns into lists is functional if the .csv has been previously download but what I'm trying to do is to get the data from the url into Python directly.

The error I get is "Attribute Error: 'module' object has no attribute 'request'." Here is the code:

import urllib

def data_pull():
#gets data out of a .csv file from yahoo finance, separates specific columns into lists

    datafile = urllib.request.urlretrieve('http://ichart.finance.yahoo.com/table.csv?s=xom&a=00&b=2&c=1999&d=01&e=12&f=2014&g=m&ignore=.csv')
    datafile = open(datafile)

    datelist = [] #blank list for dates
    pricelist = [] #blank list for prices
    for row in datafile:
        datelist.append(row.strip().split(","))
        pricelist.append(row.strip().split(","))

    datelist = zip(*datelist) #rows into columns
    datelist = datelist[0] #turns the list into data from the first column

    pricelist = zip(*pricelist)
    pricelist = pricelist[4] #list gets data from the fifth column

    print datelist
    print pricelist

data_pull()

I'm brand new to Python and coding in general. I know there are probably more efficient ways of executing the code above but my main concern is getting the urllib piece to function correctly. Thanks in advance for your comments.

Was it helpful?

Solution

You need to import the full module:

import urllib.request

If you don't, the parent package will not have the submodule as an attribute.

You probably don't want to use urllib.request.urlretrieve() here; you'd normally process the response directly in Python. You can also make use of the csv module to read the data without needing to split:

from urllib.request import urlopen
import io
import csv

url = 'http://ichart.finance.yahoo.com/table.csv?s=xom&a=00&b=2&c=1999&d=01&e=12&f=2014&g=m&ignore=.csv'
reader_input = io.TextIOWrapper(urlopen(url), encoding='utf8', newline='')
reader = csv.reader(reader_input)
next(reader, None) # skip headers
cols = list(zip(*reader))
datelist, pricelist = cols[0], cols[4]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top