IngestionTimestamp 包含 %3A 符号而不是分号

问题描述 投票:0回答:1

在我的 Azure Blob 存储中,我有一个名为 IngestionTimestamp、ISO 8601 格式的文件夹。但我想在时间部分加上分号。相反,我得到了 %3A 符号。

我尝试使用 urllib.parse unquote Python 库来解码字符串。

另外,我使用日期时间库来格式化时间戳

current_timestamp_str = current_timestamp.strftime('%Y-%m-%dT%H:%M:%S')

decoded_timestamp = unquote(current_timestamp_str + '+00:00')

在 VS Code 中,它向我显示带有分号的时间戳。但是,当我将数据放入 Blob 容器时,文件夹名称如下:IngestionTimestamp=2021-01-01T01%3A20%3A33。

我想要这个:IngestionTimestamp=2021-01-01T01:20:33+00:00

转换日期/时间格式的代码:

    current_date = date.today()
    current_date_str = str(current_date)
    current_timestamp = datetime.now(timezone.utc)
    current_timestamp_str = current_timestamp.strftime('%Y-%m-%dT%H:%M:%S')
    decoded_timestamp = unquote(current_timestamp_str + '+00:00')
python azure azure-blob-storage urllib azure-storage-explorer
1个回答
0
投票

在 VS Code 中,它显示带有分号的时间戳。但是,当我将数据放入 Blob 容器时,文件夹名称如下:IngestionTimestamp=2021-01-01T01%3A20%3A33。我想要这个:IngestionTimestamp=2021-01-01T01:20:33+00:00

您可以使用下面的代码使用Python在Azure存储中创建具有您特定格式的目录(文件夹)。

代码:

from datetime import datetime, timezone
import urllib.parse
from azure.storage.filedatalake import DataLakeServiceClient

current_timestamp = datetime.now(timezone.utc)
current_timestamp_str = current_timestamp.strftime('%Y-%m-%dT%H:%M:%S').replace(';', ':')
encoded_timestamp_str = urllib.parse.quote(current_timestamp_str + '+00:00', safe=';/?:@&=+$,-_.!~*()')

folder_name = 'IngestionTimestamp=' + encoded_timestamp_str

connection_string = 'xxxxx'
filesystem_name = 'data'

service_client = DataLakeServiceClient.from_connection_string(conn_str=connection_string)
file_system_client = service_client.get_file_system_client(filesystem_name)
directory_client = file_system_client.get_directory_client(folder_name)
directory_client.create_directory()

输出:

IngestionTimestamp directory

© www.soinside.com 2019 - 2024. All rights reserved.