本文介紹了在 Python 中從 FTP 服務器讀取文件到 DataFrame的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想將文件從 FTP 服務器加載到 Pandas 數(shù)據(jù)幀中,而無需先將其下載到磁盤.我寫了一個腳本來執(zhí)行這個命令,但下載到磁盤.這可能在 ftplib 庫中嗎?你覺得這個問題有什么解決辦法嗎?
I would like to load a file from an FTP server into Pandas dataframe without downloading it to disk first. I have written a script that executes this command but with downloading to disk. Is this possible in the ftplib library? Do you see any solution to this problem?
from ftplib import FTP
import os
import pandas as pd
ftps = FTP('gssc.esa.int')
ftps.login()
ftps.cwd('/gnss/data/daily/2019/001/')
filename = '19001.V3status'
local_filename = os.path.join(r"C:/path/where/download/files", filename) #example
lf = open(local_filename, "wb")
ftps.retrbinary('RETR ' + filename, lf.write)
file = "C:/path/where/download/files/" +filename #example
dataV3status = pd.read_fwf(file,
names = ('Mon_ID', 'Full_Mon_ID', 'RNX_Ver.', 'Dly(H)',
'Dly(M)', 'V', 'Receiver_Type', 'Antenna_Type',
'Mkr_Name', 'Marker_Number', 'Typ', 'G', 'R',
'E', 'C', 'J', 'S', 'I', 'MD5_Checksum'),
widths = [5,9,5,5,6,2,20,22,5,10,3,3,2,2,2,2,2,2,32],
header = None,
skiprows = 5,
skipfooter = 16)
推薦答案
如果你想堅持使用 ftplib,你可以這樣做:
If you want to stick with ftplib, you can do something like this:
from io import BytesIO
flo = BytesIO()
ftp.retrbinary('RETR ' + filename, flo.write)
flo.seek(0)
pd.read_fwf(flo, ...)
這篇關于在 Python 中從 FTP 服務器讀取文件到 DataFrame的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!