简体   繁体   English

如何使用Python从.CSV文件中收集数据?

[英]How should one go about collecting data from a .CSV file using Python?

I am attempting to use the Yahoo! 我正在尝试使用Yahoo! Finance API to gather current stock quotes using Python, a language I am particularly new to. Finance API使用Python(我特别陌生的一种语言)来收集当前的股票报价。 The Yahoo! 雅虎! Finance API appears to offer their data in the form of a .CSV file that can be downloaded. Finance API似乎以可以下载的.CSV文件的形式提供其数据。

I am wondering what the best way to use this data is? 我想知道使用此数据的最佳方法是什么? Is it inefficient to download the file and then read it; 下载文件然后阅读它效率低下吗? is there a method to convert it to a JSON or XML file that I can parse using something like urllib? 有没有一种方法可以将其转换为我可以使用类似urllib解析的JSON或XML文件?

The .CSV I am getting is being generated on the following page, in this case the quote for Microsoft (MSFT): 我正在获取的.CSV会在下一页生成,在这种情况下,Microsoft(MSFT)的报价是:

http://finance.yahoo.com/d/quotes.csv?s=MSFT&f=snl1 http://finance.yahoo.com/d/quotes.csv?s=MSFT&f=snl1

Many thanks in advance. 提前谢谢了。

Python has a built in csv module. Python具有内置的csv模块。

http://docs.python.org/library/csv.html http://docs.python.org/library/csv.html

To answer your downloading question: 要回答您的下载问题:

file = r"http://finance.yahoo.com/d/quotes.csv?s=MSFT&f=snl1"

import urllib
text = urllib.urlopen(file).read()

>>> print text 
... "MSFT","Microsoft Corpora",29.51

Based on csv module and DictReader , it will be more easy to parse data after urlopen, I reuse the code above from @kreativitea 基于csv模块和DictReader ,在urlopen之后解析数据会更加容易,我重用了@kreativitea中的代码

import csv
import urllib

file = r"http://finance.yahoo.com/d/quotes.csv?s=MSFT&f=snl1"

quotefile = urllib.urlopen(file)
fieldheaders = ["abbr","name","index"]
reader = csv.DictReader(quotefile,fieldnames=fieldheaders)

for row in reader:
    print row

the result is 结果是

$ quote.py
{'index': '29.51', 'abbr': 'MSFT', 'name': 'Microsoft Corpora'}

the row in for loop is the hash table, which is easy to deal with for循环中的row是哈希表,很容易处理

暂无
暂无

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

相关问题 如何在.csv文件中分离数据? - How to go about separating data in a .csv file? 从一个 go 使用 ZA7F5F354213B5274117392 的邮件附件中提取 ZIP 文件中的 CSV 文件 - Extracting CSV file inside ZIP file from mails' attachments in one go using Python 使用 Python 将数据从一个 CSV 文件写入另一个 CSV 文件 - Writing data from one CSV file to another CSV file using Python 从一个csv文件中提取指定的数据,然后使用python分配给另一个csv文件 - Extract designated data from one csv file then assign to another csv file using python 如何使用python从csv文件中检索一列? - How to retrieve one column from csv file using python? 如何使用python tkinter的mainloop维护跳动作的侦听器帧回调? - How should one go about maintaining leap motion's listener frame callback with python tkinter's mainloop? 如何使用 python 将三个 csv 文件数据写入一个具有一个日期列和三个数据列的 csv 文件 - how to write three csv file data into one csv file with one date column and three data column using python 如何使用python将所需的行从一个csv文件复制到另一个csv文件? - How do I copy only the required rows from one csv file to other csv file using python? python 如何使用 csv 文件中的数据运行函数 - python How to run a function using data from csv file 如何使用python从csv文件格式中读取数据 - How to read data from csv file format by using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM