Phi*_*ats 4 python dropbox stringio pandas
我有一个简单的任务:在 luigi 中,使用 dropbox-python sdk 将 Pandas 数据帧作为 csv 存储在 dropbox 中
通常(例如,使用 S3)您可以将 StringIO 用作类似文件的内存中对象。它也适用于熊猫df.to_csv()
不幸的是,dropbox sdk 需要二进制类型,我不知道如何将 StringIO 转换为二进制:
with io.StringIO() as f:
DF.to_csv(f, index=None)
self.client.files_upload(f, path=path, mode=wmode)
TypeError: expected request_binary as binary type, got <class '_io.StringIO'>
Run Code Online (Sandbox Code Playgroud)
ByteIO 不适用于df.to_csv()...
正如在这个答案中看到的,dropbox 需要一个字节对象,而不是一个文件,所以转换为BytesIO将无济于事。然而,解决方案比这更简单。您需要做的就是将字符串编码为二进制:
csv_str = DF.to_csv(index=None) # get string, rather than StringIO object
self.client.files_upload(csv_str.encode(), path=path, mode=wmode)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3822 次 |
| 最近记录: |