简体   繁体   English

无法在 azure 函数中使用 pandas 读取 csv 和 python

[英]Can not read csv with pandas in azure functions with python

I have created an Azure Blob Storage Trigger in Azure function in python. A CSV file adds in blob storage and I try to read it with pandas.我在 Azure function python 中创建了一个 Azure Blob 存储触发器。一个 CSV 文件添加到 Blob 存储中,我尝试用 pandas 读取它。

import logging
import pandas as pd

import azure.functions as func


def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")

    df_new = pd.read_csv(myblob)
    print(df_new.head())

If I pass myblob to pd.read_csv , then I get UnsupportedOperation: read1如果我将myblob传递给pd.read_csv ,那么我会得到UnsupportedOperation: read1

Python blob trigger function processed blob 
Name: samples-workitems/Data_26112022_080027.csv
Blob Size: None bytes
[2022-11-27T16:19:25.650Z] Executed 'Functions.BlobTrigger1' (Failed, Id=2df388f5-a8dc-4554-80fa-f809cfaeedfe, Duration=1472ms)
[2022-11-27T16:19:25.655Z] System.Private.CoreLib: Exception while executing function: Functions.BlobTrigger1. System.Private.CoreLib: Result: Failure
Exception: UnsupportedOperation: read1

If I pass myblob.read() ,如果我通过myblob.read()

df_new = pd.read_csv(myblob.read())

it gives TypeError: Expected file path name or file-like object, got <class 'bytes'> type它给出TypeError: Expected file path name or file-like object, got <class 'bytes'> type

Python blob trigger function processed blob 
Name: samples-workitems/Data_26112022_080027.csv
Blob Size: None bytes
[2022-11-27T16:09:56.513Z] Executed 'Functions.BlobTrigger1' (Failed, Id=e3825c28-7538-4e30-bad2-2526f9811697, Duration=1468ms)
[2022-11-27T16:09:56.518Z] System.Private.CoreLib: Exception while executing function: Functions.BlobTrigger1. System.Private.CoreLib: Result: Failure
Exception: TypeError: Expected file path name or file-like object, got <class 'bytes'> type

From Azure functions Docs :来自Azure 函数文档

InputStream is File-like object representing an input blob. InputStream 是 File-like object 表示输入 blob。

From Pandas read_csv Docs :来自Pandas read_csv 文档

read_csv takes filepath_or_bufferstr, path object or file-like object read_csv 采用 filepath_or_bufferstr,路径 object 或类似文件 object

So technically I should read this object. What piece of puzzle am I missing here?所以从技术上讲,我应该阅读这个 object。我在这里缺少什么拼图?

If you refer to this article , it says that this piece of code will work.如果你参考这篇文章,它说这段代码可以工作。 But this is recommended for smaller files as the entire files goes into memory. Not recommended to be used for larger files.但建议用于较小的文件,因为整个文件进入 memory。不建议用于较大的文件。

import logging
import pandas as pd

import azure.functions as func
from io import BytesIO

def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
    df_new = pd.read_csv(BytesIO(myblob.read()))
    print(df_new.head())

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

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