2013-02-18 27 views
2

我刚刚掌握了熊猫(这很棒),我需要做的是从压缩基因组文件中读取ftp站点到熊猫数据框中的文件。 这是我尝试过,并获得一吨的错误:使用大熊猫通过FTP检索文件

from pandas.io.parsers import * 

chr1 = 'ftp://ftp.ncbi.nih.gov/snp/organisms/human_9606/chr_rpts/chr_1.txt.gz' 

CHR1 = read_csv(chr1, sep='\t', compression = 'gzip', skiprows = 10) 

print type(CHR1) 
print CHR1.head(10) 

理想我想这样做:

from pandas.io.data import * 
AAPL = DataReader('AAPL', 'yahoo', start = '01/01/2006') 
+0

我不认为熊猫足够智能,可以使用FTP检索文件。 – 2013-02-18 21:01:08

回答

1

这个问题的有趣的部分是如何流(GZ)从FTP文件,该文件讨论here,在那里它声称,以下将在Python工作3.2(但won't in 2.x, nor will it be backported),和我的系统是这样的话:

import urllib.request as ur 
from gzip import GzipFile 

req = ur.Request(chr1) # gz file on ftp (ensure startswith 'ftp://') 
z_f = ur.urlopen(req) 

# this line *may* work (but I haven't been able to confirm it) 
# df = pd.read_csv(z_f, sep='\t', compression='gzip', skiprows=10) 

# this works (*) 
f = GzipFile(fileobj=z_f, mode="r") 
df = pd.read_csv(f, sep='\t', skiprows=10) 

(*)这里的f是“类文件”,因为我们可以执行readline(逐行读取),而不必下载/打开整个文件。

注:我无法得到ftplib libraryreadline,它是否应该。