久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 FTP python 讀取緩沖區(qū)中的文件

Read a file in buffer from FTP python(從 FTP python 讀取緩沖區(qū)中的文件)
本文介紹了從 FTP python 讀取緩沖區(qū)中的文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試從 FTP 服務器讀取文件.該文件是一個 .gz 文件.我想知道我是否可以在套接字打開時對此文件執(zhí)行操作.我試圖遵循 讀取文件而不寫入磁盤和從 FTP 讀取文件而不下載但不成功.

I am trying to read a file from an FTP server. The file is a .gz file. I would like to know if I can perform actions on this file while the socket is open. I tried to follow what was mentioned in two StackOverflow questions on reading files without writing to disk and reading files from FTP without downloading but was not successful.

我知道如何在下載的文件上提取數(shù)據(jù)/工作,但我不確定我是否可以即時完成.有沒有辦法連接到站點,在緩沖區(qū)中獲取數(shù)據(jù),可能進行一些數(shù)據(jù)提取并退出?

I know how to extract data/work on the downloaded file but I'm not sure if I can do it on the fly. Is there a way to connect to the site, get data in a buffer, possibly do some data extraction and exit?

嘗試 StringIO 時出現(xiàn)錯誤:

When trying StringIO I got the error:

>>> from ftplib import FTP
>>> from StringIO import StringIO
>>> ftp = FTP('ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/PMC-ids.csv.gz')

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
ftp = FTP('ftp://ftp.ncbi.nlm.nih.gov/pub/pmc/PMC-ids.csv.gz')
File "C:Python27libftplib.py", line 117, in __init__
self.connect(host)
File "C:Python27libftplib.py", line 132, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "C:Python27libsocket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 11004] getaddrinfo failed

我只需要知道如何將數(shù)據(jù)放入某個變量并在其上循環(huán),直到讀取來自 FTP 的文件.

I just need to know how can I get data into some variable and loop on it until the file from FTP is read.

感謝您的寶貴時間和幫助.謝謝!

I appreciate your time and help. Thanks!

推薦答案

請務必先登錄ftp服務器.之后,使用 retrbinary 以二進制模式拉取文件.它對文件的每個塊使用回調(diào).您可以使用它來將其加載到字符串中.

Make sure to login to the ftp server first. After this, use retrbinary which pulls the file in binary mode. It uses a callback on each chunk of the file. You can use this to load it into a string.

from ftplib import FTP
ftp = FTP('ftp.ncbi.nlm.nih.gov')
ftp.login() # Username: anonymous password: anonymous@

# Setup a cheap way to catch the data (could use StringIO too)
data = []
def handle_binary(more_data):
    data.append(more_data)

resp = ftp.retrbinary("RETR pub/pmc/PMC-ids.csv.gz", callback=handle_binary)
data = "".join(data)

加分項:我們在解壓字符串時如何?

Bonus points: how about we decompress the string while we're at it?

簡單模式,使用上面的數(shù)據(jù)字符串

Easy mode, using data string above

import gzip
import StringIO
zippy = gzip.GzipFile(fileobj=StringIO.StringIO(data))
uncompressed_data = zippy.read()

稍微好一點,完整的解決方案:

from ftplib import FTP
import gzip
import StringIO

ftp = FTP('ftp.ncbi.nlm.nih.gov')
ftp.login() # Username: anonymous password: anonymous@

sio = StringIO.StringIO()
def handle_binary(more_data):
    sio.write(more_data)

resp = ftp.retrbinary("RETR pub/pmc/PMC-ids.csv.gz", callback=handle_binary)
sio.seek(0) # Go back to the start
zippy = gzip.GzipFile(fileobj=sio)

uncompressed = zippy.read()

實際上,動態(tài)解壓縮會好得多,但我看不到使用內(nèi)置庫的方法(至少不容易).

In reality, it would be much better to decompress on the fly but I don't see a way to do that with the built in libraries (at least not easily).

這篇關于從 FTP python 讀取緩沖區(qū)中的文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標)
主站蜘蛛池模板: 青娱乐av| 国产九九热 | 91久久国产综合久久91 | 九一九色国产 | 日韩 欧美 | 一级黄色片视频 | 国产1级片 | 国产精品久久久久久久久久 | 久久综合久色欧美综合狠狠 | 久久精品一区二区国产 | 午夜精品国产精品大乳美女 | 亚洲小视频在线观看 | 亚洲精品综合 | 九色视频丨porny丨丝袜 | www.超碰 | 国产蜜臀av| 成人理论影院 | 一级免费黄色片 | 看一级黄色片 | 欧美不卡 | 黄色一级片网站 | 亚洲精品www久久久久久广东 | 日日干天天操 | av片免费看| 欧美一区二区在线观看 | 国产suv精品一区 | 久久精品欧美一区二区 | 亚洲成人免费在线观看 | 成人三级在线 | 欧美视频在线观看 | 操操操干干干 | 啪啪免费网站 | 中文字幕在线观看日韩 | 国产精品免费人成网站酒店 | 久久99免费视频 | 超碰99在线 | 911看片 | 日韩欧美小视频 | 亚洲无av在线中文字幕 | 在线观看免费av网站 | 四虎av|