---
title: "如何把从雅虎财经请求到的数据储存为本地的csv文件"
author: "hellocgc"
date: "2011-01-01T22:50:59Z"
tags: []
categories: ["编程语言"]
slug: "csv"
draft: false
lastmod: "2011-01-01T22:50:59Z"
id: 857487
url: "https://hellocgc.iteye.com/blog/857487"
---

import urllib2  
import StringIO  
h = urllib2.HTTPHandler(debuglevel = 0)  
reqStockData = urllib2.Request('http://ichart.yahoo.com/table.csv?s=000001.SS&a=07&b=4&c=2010&d=11&e=31&f=2010&g=d')  
opener = urllib2.build\_opener(h)  
data = opener.open(reqStockData)  
inputStream = StringIO.StringIO(data.read())  
try:  
    f = open('d:\\\\a.csv','w')  
    while True:  
        byte = inputStream.next()  
        f.write(byte)  
except StopIteration,e:  
    f.close()

#然后可以用python自带的csv模块进行读取

import csv  
readCsv = csv.reader(open('d://a.csv'))  
for Date,Open,High,Low,Close,Volume,adjClose in readCsv:  
    print Date

进一步阅读：

[http://www.pythonclub.org/python-files/csv](http://www.pythonclub.org/python-files/csv)