我正在开发一个应用程序,用户可以在其中提供下拉框文件的路径和访问令牌并查看文件的内容。是否可以只读取文件的内容而不实际下载它。
import dropbox
import tempfile
import csv
dbx = dropbox.Dropbox(<access_token>)
metadata, f = dbx.files_download('/test/MOCK_DATA.csv')
filename = tempfile.NamedTemporaryFile(suffix='.csv').name
with open(filename, 'wb') as file:
file.write(f.content)
with open(filename) as file:
csv_reader = csv.reader(file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(row)
line_count += 1
print(f'Processed {line_count} lines.')
目前我能够阅读内容,但只有在下载后。