简体   繁体   English

使用 python 创建 csv 文件并将其上传到 azure blob 存储

[英]Create and upload csv file to azure blob storage using python

I am trying to create csv file using python and than to upload that file to azure blob storage.我正在尝试使用 python 创建 csv 文件,然后将该文件上传到 azure blob 存储。 I am able to create csv file, that part works fine, but when I try to upload the file to blob:我能够创建 csv 文件,该部分工作正常,但是当我尝试将文件上传到 blob 时:

blob.upload_blob(data=df.to_csv("capacity.csv",index=False))

I get error message:我收到错误消息:

Traceback (most recent call last):
  File ".\blob.py", line 94, in <module>
    blob.upload_blob(data=df.to_csv("data.csv",index=False))
  File "C:\Python38\lib\site-packages\azure\core\tracing\decorator.py", line 83, in wrapper_use_tracer    return func(*args, **kwargs)
  File "C:\Python38\lib\site-packages\azure\storage\blob\_blob_client.py", line 489, in upload_blob   
    options = self._upload_blob_options(
  File "C:\Python38\lib\site-packages\azure\storage\blob\_blob_client.py", line 332, in _upload_blob_options
    raise TypeError("Unsupported data type: {}".format(type(data)))
TypeError: Unsupported data type: <class 'NoneType'>

After that I tried to upload some local csv file, just to test if it is working:之后我尝试上传一些本地 csv 文件,只是为了测试它是否工作:

with open(path_to_file, "rb") as data:
   blob.upload_blob( data=data)

And it worked.它奏效了。 I am not sure what is causing this error, I spent some time looking for solution, but nothing is working for me.我不确定是什么导致了这个错误,我花了一些时间寻找解决方案,但没有什么对我有用。

You are converting a .csv-File to csv again:您再次将.csv-File转换为 csv:

blob.upload_blob(data=df.to_csv("capacity.csv",index=False))
                                 ^

But you should convert your DataFrame to csv:但是您应该将 DataFrame 转换为 csv:

blob.upload_blob(data=df.to_csv(index=False))
                                

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

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