python - 用于根据python中的指定日期在azure容器中定位数据的函数
问题描述
我在 azure 上有一个容器,它以这种格式将 json 文件保存在一个目录中:
['machine_name/machine/Year/Month/machine_YearMonthDay_00.json']
. 这些文件是根据它们创建的Year Month
和命名的。Day
因此,例如,如果我们今天创建了一个 json 文件,它将被保存在容器的目录中,如下所示:machine_name/machine/2020/**08**/machine_**20200822**_00.json
另一个例子,如果我们有一个在9 月 2 日创建的 json 文件,它将被保存在容器的目录中:machine_name/machine/2020/**09**/machine_**20200902**_00.json
这里真正的问题是能够编写一个函数,根据指定的日期列出来自不同月份的所有 json 文件。这是我编写的代码示例:
def specified_dates(startdate, enddate):
container = ContainerClient.from_container_url(SAS_URI)
blob_root = f"{'machine_name'}/{'machine'}/{startdate.split('/')[2]}/{startdate.split('/')[0]}/"
blob_list_month = [blob.name for blob in container.list_blobs(name_starts_with=blob_root)]
blob_list= [blobname for blobname in list_dates if int(blobname.split('/')[-1].split('_')[1][-2:])>=int(startdate.split('/')[1])
and int(blobname.split('/')[-1].split('_')[1][-2:])<= int(enddate.split('/')[1])]
display(blob_list)
specified_dates('07/26/2020', '07/29/2020')
到目前为止,我尝试的是列出同一个月内的所有数据。在这种情况下,它将根据给定日期列出所有 json 文件Month 07
。
我得到的结果如下:
['machine_name/machine/2020/07/machine_20200726_00.json',
'machine_name/machine/2020/07/machine_20200727_00.json',
'machine_name/machine/2020/07/machine_20200728_00.json',
'machine_name/machine/2020/07/machine_20200729_00.json']
但是我需要的是能够根据不同月份列出所有 json 文件。因此,例如,如果我需要列出所有 json 文件07/29/2020
到08/02/2020
预期结果应该如下:
['machine_name/machine/2020/07/machine_20200729_00.json',
'machine_name/machine/2020/07/machine_20200730_00.json',
'machine_name/machine/2020/07/machine_20200731_00.json',
'machine_name/machine/2020/08/machine_20200801_00.json',
'machine_name/machine/2020/08/machine_20200802_00.json']
我应该如何修改代码以便从容器访问不同的月份?如果有人能给我答案,我将不胜感激。
解决方案
您可以使用这样的解决方案:首先,列出容器中的所有文件 -> 然后从文件中获取日期(如 20200729) -> 最后,将日期与开始/结束日期进行比较。
这是一个示例代码,请随时修改它以满足您的需要:
from azure.storage.blob import BlobServiceClient
connect_str="xxx"
container_name="vm1"
startdate="07/29/2020"
enddate="09/02/2020"
#change the start and end date to this format like 20200726, so it can be used to compare the date in file name.
format_startdate=startdate.split('/')[2] + startdate.split('/')[0] + startdate.split('/')[1]
format_enddate=enddate.split('/')[2] + enddate.split('/')[0] + enddate.split('/')[1]
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container_name)
blobs = container_client.list_blobs()
for b in blobs:
#get the date from the file name
thedate = b.name.split('/')[-1].split('_')[1]
if thedate >= format_startdate and thedate <= format_enddate:
print(b.name)
print("**completed**")
推荐阅读
- c++ - 转换、强制转换和强制转换有什么区别?
- python - 使用 optuna 从 python 成功访问 MySQL 数据库
- kubernetes - 我们如何正确分配 pod,以便 KFServing 可以将 GPU 实例缩减到零?
- java - 为什么 DeepLearning4J CNN 在 INDArray 输出中不返回概率,而只返回 0 和 1
- python - python os.system("chcp 65001") 不起作用
- regex - 悬空元字符 * sparksql
- node.js - 导航部分的通用正确链接(express.js 和车把)
- html - 引导设计查询
- php - 将变量从 jQuery 保存到 PHP
- airflow - 气流返回列表索引超出范围