熊猫:我正在尝试打开存储在ftp站点上的zip文件中的特定.txt文件

时间:2016-07-01 10:39:52

标签: python pandas ftp zip

我需要在this ftp站点的每个zip文件中将每个produkt_monat_Monatswerte_18910101_20110331_00003.txt文件连接成一个单独的框架。

这是我目前使用的代码:

    import pandas as pd
    from pandas.io.parsers import *
    import glob
    import requests
    from zipfile import ZipFile
    import urllib.request as ur


    years = 'produkt_monat_Monatswerte_*.txt'

names = pd.DataFrame()
for year in years:
    path ="ftp://ftp-cdc.dwd.de/pub/CDC/observations_germany/climate/monthly/kl/historical/monatswerte_?????_????????_????????_hist.zip").read()
    frame = pd.read_csv(path, names=columns)

    frame['year'] = year
    names = names.concat(frame, ignore_index=True)

它给了我以下错误:

 File "<ipython-input-25-d57a1d77ecc6>", line 5
    path ="ftp://ftp-cdc.dwd.de/pub/CDC/observations_germany/climate/monthly/kl/historical/monatswerte_?????_????????_????????_hist.zip")

1 个答案:

答案 0 :(得分:1)

问题是你不能让pandas从Zip中提取内部文件。 请尝试以下代码:

import pandas as pd
from ftplib import FTP
import os
from zipfile import ZipFile
from io import BytesIO

f_root = 'ftp-cdc.dwd.de'
zips_path = '/pub/CDC/observations_germany/climate/monthly/kl/historical/'

ftp = FTP(f_root)

ftp.login()

ftp.cwd(zips_path)

paths = [p[0] for p in ftp.mlsd('.') if p[0].endswith('.zip')]

dfs = []

for path in paths:
    buf = BytesIO()
    ftp.retrbinary("RETR " + path, lambda block: buf.write(block))
    z = ZipFile(buf)

    zi = list(filter(lambda x: x.filename.startswith('produkt'), z.filelist))[0]
    df = pd.read_csv(BytesIO(z.read(zi.filename)), sep=';', encoding="cp1252")
    dfs.append(df)

final = pd.concat(dfs)