如何解决处理后将文件从 azure 存储容器上传到另一个容器的问题
场景是: 我在 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打开这个文件时,找不到文件。
结果图像:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。