本文介紹了在 Python 中從 FTP 服務(wù)器讀取文件到 DataFrame的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我想將文件從 FTP 服務(wù)器加載到 Pandas 數(shù)據(jù)幀中,而無(wú)需先將其下載到磁盤.我寫了一個(gè)腳本來(lái)執(zhí)行這個(gè)命令,但下載到磁盤.這可能在 ftplib 庫(kù)中嗎?你覺(jué)得這個(gè)問(wèn)題有什么解決辦法嗎?
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)
推薦答案
如果你想堅(jiān)持使用 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, ...)
這篇關(guān)于在 Python 中從 FTP 服務(wù)器讀取文件到 DataFrame的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!