简体   繁体   English

使用Python将来自Yahoo Finance的.csv转换为列列表

[英]Turning a .csv from yahoo finance into lists of columns with Python

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. 我试图以.csv的形式从Yahoo Finance中提取数据,然后将第1列和第5列转换为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. 如果.csv先前已下载,则将列转换为列表的代码部分是可以使用的,但是我想做的是将URL中的数据直接导入Python。

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. 我是Python和编码方面的新手。 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. 我知道可能有更有效的方法来执行上述代码,但是我主要关心的是让urllib片段正确运行。 Thanks in advance for your comments. 预先感谢您的评论。

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; 您可能不想在这里使用urllib.request.urlretrieve() you'd normally process the response directly in Python. 您通常可以直接在Python中处理响应。 You can also make use of the csv module to read the data without needing to split: 您也可以使用csv模块读取数据,而无需拆分:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM