【问题标题】:Read a CSV file stored in a FTP in Python在 Python 中读取存储在 FTP 中的 CSV 文件
【发布时间】:2018-07-26 16:11:09
【问题描述】:

我已连接到 FTP 并且连接成功。

import ftplib
ftp = ftplib.FTP('***', '****','****')
listoffiles = ftp.dir()
print (listoffiles)

我在这个 FTP 中有一些 CSV 文件和一些包含更多 CSV 的文件夹。

我需要识别此位置(家)中的文件夹列表,并需要导航到文件夹中。我认为cwd 命令应该可以工作。

我还读取了存储在此 FTP 中的 CSV。我怎样才能做到这一点?有没有办法将这里的 CSV 文件直接加载到 Pandas 中?

【问题讨论】:

标签: python pandas csv ftp ftplib


【解决方案1】:

根据这里的答案 (Python write create file directly in FTP) 和我自己对 ftplib 的了解:

您可以执行以下操作:

from ftplib import FTP
import io, pandas

session = FTP('***', '****','****')

# get filenames on ftp home/root
remoteFilenames = session.nlst()
if ".." in remoteFilenames:
    remoteFilenames.remove("..")
if "." in remoteFilenames:
    remoteFilenames.remove(".")
# iterate over filenames and check which ones are folder
for filename in remoteFilenames:
    dirTest = session.nlst(filename)
    # This dir test does not work on certain servers
    if dirTest and len(dirTest) > 1:
        # its a directory => go to directory
        session.cwd(filename)
        # get filename for on ftp one level deeper
        remoteFilenames2 = session.nlst()
        if ".." in remoteFilenames2:
            remoteFilenames2.remove("..")
        if "." in remoteFilenames2:
            remoteFilenames2.remove(".")
        for filename in remoteFilenames2:
            # check again if the filename is a directory and this time ignore it in this case
            dirTest = session.nlst(filename)
            if dirTest and len(dirTest) > 1:
                continue

            # download the file but first create a virtual file object for it
            download_file = io.BytesIO()
            session.retrbinary("RETR {}".format(filename), download_file.write)
            download_file.seek(0) # after writing go back to the start of the virtual file
            pandas.read_csv(download_file) # read virtual file into pandas
            ##########
            # do your thing with pandas here
            ##########
            download_file.close() # close virtual file

session.quit() # close the ftp session

或者,如果您知道 ftpserver 的结构,您可以遍历具有文件夹/文件结构的字典并通过 ftplib 或 urllib 下载文件,如示例中所示:

for folder in {"folder1": ["file1", "file2"], "folder2": ["file1"]}:
    for file in folder:
        path = "/{}/{}".format(folder, file)
        ##########
        # specific ftp file download stuff here
        ##########
        ##########
        # do your thing with pandas here
        ##########

这两种解决方案都可以通过使它们递归或一般支持多于一级的文件夹来优化

【讨论】:

  • 当我使用nlst() 方法时,它只返回一个文件列表,没有目录。因此,它不会向下钻取子目录,并且由于我的根目录是空的,remoteFilenames = session.nlst() 返回一个空列表。
【解决方案2】:

迟到总比没有好...我能够直接读入熊猫。不确定这是否适用于任何人。

import pandas as pd
from ftplib import FTP
ftp = FTP('ftp.[domain].com') # you need to put in your correct ftp domain
ftp.login() # i don't need login info for my ftp
ftp.cwd('[Directory]') # change directory to where the file is
df = pd.read_csv("[file.csv]", delimiter = "|", encoding='latin1') # i needed to specify delimiter and encoding
df.head()

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 2016-10-30
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多