[英]Read xlsx from azure blob storage to pandas dataframe without creating temporary file
[英]How to read .xlsx format file from Azure Blob using pandas without creating temporary file
下面是我的代碼。
from azure.storage.blob import BlobClient, BlobServiceClient
import pandas as pd
from io import StringIO
account_url = "XXXXXX"
containerName = "XXXXXX"
blob_name = 'Abc.xlsx'
storageKey = "XXXXX"
connection_string = "XXXX"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(container=containerName, blob=blob_name)
blob = blob_client.download_blob().content_as_text(encoding=None)
engine = 'xlrd'
df = pd.read_excel(io = blob, engine = engine)
當我運行它時,代碼拋出異常:
raise ValueError('Must explicitly set engine if not passing in'
ValueError: Must explicitly set engine if not passing in buffer or path for io.
任何幫助,將不勝感激。
下面的代碼使用內存中的流下載而不創建臨時文件,pandas 從流中讀取 excel。
from azure.storage.blob import BlobServiceClient
import pandas as pd
from io import BytesIO
containerName = "XXXX"
blob_name = 'Abc.xlsx'
connection_string = "XXXX"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
blob_client = blob_service_client.get_blob_client(containerName, blob_name)
with BytesIO() as input_stream:
blob = blob_client.download_blob().download_to_stream(input_stream)
df = pd.read_excel(io=input_stream)
print(df)
data=pd.read_excel("C:\\ex1.xlsx") 或 data=pd.read_excel(r"C:\\ex1.xlsx") #如果你使用Windows,有時需要
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.