简体   繁体   English

熊猫和蔚蓝:DataFrame to_csv blob

[英]pandas and azure: DataFrame to_csv blob

I want to store a python pandas DataFrame to Azure, but the solution by Jay Gong (Python 2.7) gives me errors: 我想将python pandas DataFrame存储到Azure,但是Jay Gong (Python 2.7)的解决方案给了我错误:

from azure.storage.blob import BlockBlobService
service = BlockBlobService(
    account_name=STORAGE_ACCOUNT_NAME,
    account_key=STORAGE_ACCOUNT_KEY
)

with io.StringIO() as stream:
    df.to_csv(stream, encoding="utf-8")
    service.create_blob_from_text('containert', 'example.txt', stream)

Returns: 返回:

AttributeError: '_io.StringIO' object has no attribute 'encode' AttributeError:'_io.StringIO'对象没有属性'encode'

  • python 3.7 python 3.7
  • pandas 0.23.4 熊猫0.23.4
  • azure-blob-storage 1.3.1 天蓝色斑点存储1.3.1

This works, but is not really elegant: 这可行,但并不十分优雅:

with io.StringIO() as streamio:
    df.to_csv(streamio, encoding = "utf-8", index=False)
    streamio.seek(0)
    service.create_blob_from_text('containert', 'example.txt', "".join(streamio.readlines()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM