开发者

Python change data into sequence

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

Python URL download


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜