tags:

views:

55

answers:

2

The "idata" I pulled from this URL needs to be turned into a sequence, how do i turn it into a sequence

import urllib, csv

URL = "http://download.finance.yahoo.com/d/quotes.csv?s=%s&f=sl1vt1&e=.csv"
symbols = ('GGP', 'JPM', 'AIG', 'AMZN','GGP', 'JPM', 'AIG', 'AMZN')
#symbols = ('GGP',)

def fetch_quote(symbols):
    url = URL % '+'.join(symbols)
    fp = urllib.urlopen(url)
    try:
        data = fp.read()
    finally:
        fp.close()
    return data # <======== Return

idata = fetch_quote(symbols)
print idata

http://stackoverflow.com/questions/3637553/python-url-download

+1  A: 

I would guess based on the URL that you are downloading data in CSV format. As such, you probably want to parse it with a CSV reader.

Seth
"for row in idata: print row gives me weird data
Define weird? What does the data look like when you just download it in a browser?
Seth
the data goes one character straight down.
+1  A: 

What do you mean by "a sequence"? You could turn it into a dictionary as follows. Just place this code after you produce idata.

stocks = {}
for line in idata.split("\r\n"):
    if line == '':
        continue

    stock, price, volume, stime = line.split(',')
    stock = stock[1:-1]
    price = float(price)
    volume = int(volume)
    stime = stime[1:-1]

    stocks[stock] = (price, volume, stime)

If you want to be a bit more robust, you can use the csv module (add import csv to the top of your code) then use

reader = csv.reader(idata.split("\r\n"))

stocks = {}
for line in reader:
    if line == '':
        continue

    stock, price, volume, stime = line
    price = float(price)
    volume = int(volume)

    stocks[stock] = (price, volume, stime)

For inserting into a database, the following may work

reader = csv.reader(idata.split("\r\n"))

stocks = []
for line in reader:
    if line == '':
        continue

    stock, price, volume, stime = line
    price = float(price)
    volume = int(volume)

    stocks.append((stock, price, volume, stime))

csr.executemany('INSERT INTO test.prices VALUES (?,?,?,?)', stocks)

This of course assumes your columns are in the same order as the elements of the array.

Michael Mior
Thanks, I am tring to Insert the data into Sqlite using csr.executemany('INSERT INTO test.prices VALUES (?,?,?,?)', idata)pyodbc.ProgrammingError: The second parameter to executemany must be a sequence.for row in idata: csr.execute('INSERT INTO test.prices VALUES (?,?,?,?)', idata)pyodbc.ProgrammingError: ('The SQL contains 4 parameter markers, but 1 parameters were supplied', 'HY000')
See my updated answer for a possible solution.
Michael Mior
thanks, for our help...... stock, price, volume, stime = lineValueError: need more than 0 values to unpack
thanks, used method 1....changed to list.....I would like to try method 3.... if you figure out error.....