Python 不显示整个 CSV 文件形式的 SFTP

Python not displaying entire CSV file form SFTP

因此,我已使用 paramiko 连接到 SFTP 服务器,并且我试图在不下载的情况下显示整个 CSV 文件。

import paramiko


host,port = 'HOST',22
transport = paramiko.Transport((host,port))


username,password = 'LOGIN','PASS'
transport.connect(None,username,password)


sftp = paramiko.SFTPClient.from_transport(transport)


listdir = sftp.listdir()


import pandas
import csv
file = sftp.open('fixed.csv')


csvreal = pandas.read_csv(file)
for row in csvreal:
    print(row)




file.close()

if sftp: sftp.close()
if transport: transport.close()

CSV文件有几百行,但是我运行这个的时候,它只显示第一行。我究竟做错了什么?不知道为什么这不应该正常工作。请告知,谢谢!

到目前为止,您根本没有使用 csv 包,也没有pandas发挥其全部功能。

如果您只是想将整行作为一个打印出来,您可以替换循环:

for index, row in csvreal.iterrows():
    print(csvreal.loc[[index]].to_string(index=False, header=False))

为了解析您的数据,您可能需要仔细查看 pandas 文档并从一些教程开始。根据您的评论,您的数据可能类似于 d,因此对于初学者,请考虑以下代码片段:

d = {'COURSENAME': ['french', 'spanish'], 'COURSECODE': ['F1', 'S2'], 'COMPLETION_DATE': ['2020-04-08', '2020-01-27']}
csvreal = pd.DataFrame(data=d)
print(csvreal)
for index, row in csvreal.iterrows():
    print('The course', row['COURSENAME'], 'was completed on', row['COMPLETION_DATE'])

根据您的数据,重置索引可能会有用。请提供有关 csv 文件结构的更多详细信息(尤其是 header)以获得更具体的答案。