-1

I have a script which get .csv file and some data correction and save my django database. In my case I couldn't get .csv file from FTP server. I tried following codes but I faced different errors each time.

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)

Error: "AttributeError: module 'pysftp' has no attribute 'connect'"

ftp = FTP('your_host')
ftp.login('your_user', 'your_pw')
ftp.set_pasv(False)

I couldn't go further.

How can I read .csv file from FTP server using by pandas?

I Solved my problem as below:

I copied files then opened as pd.

with FTP(host) as ftp:
    ftp.login(user=user, passwd=password)
    print(ftp.getwelcome())

    with open("proj.csv", "wb") as f:
        ftp.retrbinary("RETR " + "proj.csv", f.write, 1024)
    
    with open("pers.csv", "wb") as f:
        ftp.retrbinary("RETR " + "pers.csv", f.write, 1024)

    ftp.quit()

1 Answers1

0
 import pysftp
 import pandas as pd
    
 cnopts = pysftp.CnOpts()
 cnopts.hostkeys = None
 
 with pysftp.Connection(hostname='hostname',username='username',password='password', cnopts=cnopts) as conn:
    conn.get('filename')

 with.open('filename') as f:
    df = pd.read_csv(f)

this should give you the data frame of csv.

whitespace
  • 789
  • 6
  • 13
  • Thank you for your answer, but I received "UserWarning: Failed to load HostKeys from C:\Users\xxxxx\.ssh\known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None)." – Seckinyilmaz Sep 16 '21 at 12:06
  • you can check now – whitespace Sep 16 '21 at 12:10
  • I tried but "UserWarning: Failed to load HostKeys from C:\Users\xxx\.ssh\known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None)." – Seckinyilmaz Sep 16 '21 at 12:17
  • @Seckinyilmaz All your problems were asked here already. Please google the error messages your are getting before you ask! Also SFTP it not FTP. Those are completely different protocols. – Martin Prikryl Sep 16 '21 at 12:34