Paramiko成功上传文件,但它是空的

时间:2017-06-06 15:54:38

标签: python pandas sftp paramiko

尝试使用StringIO将数据帧导出到csv,并使用paramiko通过SFTP传输。该文件成功通过,但它是空的。知道为什么吗?

import pandas as pd
from StringIO import StringIO
import paramiko

names = ['Bob','Jessica','Mary','John','Mel']
births = [968, 155, 77, 578, 973]

BabyDataSet = list(zip(names,births))
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])

buf = StringIO()
df.to_csv(buf, sep = ',', header = True, index = False)
#buf.getvalue() # Correct output

transport = paramiko.Transport(('localhost', 22))
transport.connect(username='user', password='pass')
sftp = paramiko.SFTPClient.from_transport(transport)

upload_path = '/Users/user/Desktop/test.csv'
sftp.putfo(buf, upload_path)
sftp.close()

1 个答案:

答案 0 :(得分:4)

使用此行,您已写入CREATE PROCEDURE SelectTopYear AS SELECT TOP 5 StudentId, ISNULL(SUM(Score),0) As HighScoreUser FROM (SELECT StudentId, Score FROM tbl_ActPoint UNION ALL SELECT StudentId, Score FROM tbl_EvaPoint ) as T GROUP BY StudentId ORDER BY HighScoreUser DESC RETURN 0 ,就像它是一个文件一样。

buf

它是一个类似文件的对象,所以它引用了该文件的文件数量。你是。当df.to_csv(buf, sep = ',', header = True, index = False) 尝试从putfo读取时,它会从最后使用的位置读取,并且由于您已写入缓冲区,因此该位置位于数据的末尾。无论文件中的当前位置如何,buf都会返回缓冲区的整个内容。在正常阅读时寻求开始获取所有内容:

buf.getvalue()