gpt4 book ai didi

python - 处理后将文件从 azure 存储容器上传到另一个容器时出现问题

转载 作者:行者123 更新时间:2023-12-03 01:25:49 24 4
gpt4 key购买 nike

场景是:我在 Azure 存储中有一个 CSV 文件。我想处理该文件的一列(例如,在记录接收的每分钟分离并创建一个新文件),然后将新文件存储在另一个 azure 存储容器上。

在下面的代码中,我读取一个文件并处理它并创建单独的文件,但是当我想上传时收到此错误:[Errno 2] 没有这样的文件或目录:我的代码是:

import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
import pandas as pd

try:
print("Azure Blob Storage v" + __version__ + " - Python quickstart sample")

accountName = "***"
accountKey = "*****"
containerName = "sourcedata"
blobName = "testdataset.csv"
urlblob = "https://***.blob.core.windows.net/sorcedata/testdataset.csv"
connect_str = "******"

blobService = BlobServiceClient(account_name=accountName, account_key=accountKey, account_url=urlblob)


# Create the BlobServiceClient object which will be used to create a container client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

# Create a unique name for the container
container_name = str(uuid.uuid4())

# Create the container
container_client = blob_service_client.create_container(container_name)

df = pd.read_csv(urlblob)

# create datetime column
df['datetime'] = pd.to_datetime(df.received_time, format='%M:%S.%f')

# groupby with Grouper, and save to csv
for g, d in df.groupby(pd.Grouper(key='datetime', freq='1min')):
# Create file name
filename = str(g.time()).replace(':', '')
# remove datetime column and save CSV file
d.iloc[:, :-1].to_csv(f'{filename}.csv', index=False)
# Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=filename)
print("\nUploading to Azure Storage as blob:\n\t" + filename)
# Upload the created file
with open(filename, "rb") as data:
blob_client.upload_blob(data)



except Exception as ex:
print('Exception:')
print(ex)

最佳答案

你应该这样编写代码:

with open(filename + ".csv", "rb") as data:

filename 只是您的文件名,不带后缀。它是不完整的,所以当python打开这个文件时,它找不到该文件。

enter image description here

结果图像:

enter image description here

enter image description here

关于python - 处理后将文件从 azure 存储容器上传到另一个容器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66328255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com