Skip to content Skip to sidebar Skip to footer

How Do I Read A Csv From Secure Ftp Server Into A Pandas Dataframe

I have a set of CSV files on a secure FTP server that I'm trying to read into (separate) Pandas DataFrames in memory so that I can manipulate them and then pass them elsewhere via

Solution 1:

IIRC you can perform authenticated FTP requests using urllib2. Perhaps something like

import urllib2, base64
import pandas as pd

req = urllib2.Request('ftp://example.com')
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string) 
response = urllib2.urlopen(req)
data = pd.csv_read(response.read())

Not tested but you can find more information urllib2 here.

Solution 2:

I did the following, thanks to John Zwinck:

import pandas as pd
import pysftp as sftp

with sftp.connect(your_host, your_user, your_pw) as conn:
    with conn.open("path_and_file.csv", "r") as f:
        df = pd.read_csv(f)

and it worked just fine. Best regards.

Post a Comment for "How Do I Read A Csv From Secure Ftp Server Into A Pandas Dataframe"