2017-09-26 6 views
0

보안 FTP 서버에 CSV 파일 집합을 가지고있어 조작 할 수 있도록 팬더 데이터 프레임을 메모리에 읽는 중입니다. 그런 다음 API를 통해 다른 곳에 전달하십시오. FTP 서버는 인증이 필요합니다. 그렇지 않으면 매우 유용한 pd.read_csv()을 사용하여 서버에서 직접 csv를 읽을 수 없습니다.보안 FTP 서버에서 팬더 데이터 프레임으로 CSV를 읽는 방법

다음 (파이썬 3.x의) 코드를 연결 한 후 디스크에 파일을 기록합니다 :.

from ftplib import FTP 
import pandas as pd 

server = "server.ip" 
username = "user" 
password = "psswd" 

file1 = "file1.csv" # Just one of the files; I'll eventually loop through... 

ftp = FTP(server) 
ftp.login(user=username, passwd=password) 

with open(filename, "wb") as file: 
    ftp.retrbinary("RETR " + filename, file.write) 

# Do some other logic not relevant to the question 

나는 다시 그것을 읽고 다음 파일을 디스크에 쓰기 방지하고 싶습니다 I pd.read_csv()은 공개 주소에서 바로 CSV 파일을 읽을 수 있지만 파일이 로그인 뒤에 문을 열 때 어떻게 표시되는지는 알 수 없습니다.

답변

0

IIRC urllib2를 사용하여 인증 된 FTP 요청을 수행 할 수 있습니다. 아마도 뭔가 같은 것

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()) 

테스트되지 않았지만 자세한 정보는 urllib2 here을 참조하십시오.

관련 문제