日安!
我有一个如下格式的S3路径:
s3://test-bucket/test/AUS/int1/DATA/table1/effective_at_year=2019/effective_at_month=12/ndh_effective_at_day=12/ndh_effective_at_hour=03/ndh_effective_at_minute=42/
我想从上面的路径中提取日期/时间,并将其作为字符串。
我已经使用了下面的代码并实现了预期的输出,但我想知道是否有任何有效的方法来实现我所实现的内容。
pth=s3://test-bucket/test/AUS/int1/DATA/table1/effective_at_year=2019/effective_at_month=12/ndh_effective_at_day=12/ndh_effective_at_hour=03/ndh_effective_at_minute=42/
pth1=pth.split("=")[1:]
res = [int(sub.split('/')[0]) for sub in pth1]
'-'.join(str(dt) for dt in res[:3])+' '+':'.join(str(dt) for dt in res[3:])
output: '2019-12-12 3:42'你的建议将会受到高度的感谢。谢谢
发布于 2020-05-26 10:35:17
下面是如何解析和创建包含结果的datetime对象的示例:
import datetime
pth = "s3://test-bucket/test/AUS/int1/DATA/table1/effective_at_year=2019/effective_at_month=12/ndh_effective_at_day=12/ndh_effective_at_hour=03/ndh_effective_at_minute=42/"
# collect datetime components
# - assumes components are in the appropriate order
datetime_components = []
for field in pth.split('/'):
if "=" in field:
key, value = field.split("=")
datetime_components.append((key, value))
result_datetime = datetime.datetime(*[int(v) for _, v in datetime_components])这里的*运算符解压缩列表,将结果作为位置参数提供给datetime.datetime()构造函数:
result_datetime = datetime.datetime(*[int(v) for _, v in datetime_components])
# is equivalent to
desired_components = [int(v) for _, v in datetime_components]
result_datetime = datetime.datetime(desired_components[0], desired_components[1], desired_components[2], desired_components[3], desired_components[4])一旦将其放入datetime对象中,就可以根据需要轻松地对其进行操作。
# 2019-12-12 3:42
result_datetime.strftime("%Y-%m-%d %H:%M")'2019-12-12 03:42'
发布于 2020-05-26 10:35:40
我不知道更多关于AS3的细节,只有datetime部分,你可以只使用datetime.strptime。
from datetime import datetime
print(datetime.strptime('/test/AUS/int1/DATA/table1/effective_at_year=2019/effective_at_month=12/ndh_effective_at_day=12/ndh_effective_at_hour=03/ndh_effective_at_minute=42/',
'/test/AUS/int1/DATA/table1/effective_at_year=%Y/effective_at_month=%m/ndh_effective_at_day=%d/ndh_effective_at_hour=%H/ndh_effective_at_minute=%M/'))当然,您应该先删除前缀。要解析URL,可以使用urllib.parse.urlparse
from urllib.parse import urlparse
print(urlparse('s3://test-bucket/test/AUS/int1/DATA/table1/effective_at_year=2019/effective_at_month=12/ndh_effective_at_day=12/ndh_effective_at_hour=03/ndh_effective_at_minute=42/').path)发布于 2020-05-26 10:46:23
您可以使用regex提取日期:
import re
from datetime import datetime
test_str = "s3://test-bucket/test/AUS/int1/DATA/table1/effective_at_year=2019/effective_at_month=12/ndh_effective_at_day=12/ndh_effective_at_hour=03/ndh_effective_at_minute=42/"
regex = r"=(\d+)\/"
values = [
int(match.group(1))
for match in re.finditer(regex, test_str)
]
date = datetime(*values)
formated_date = date.strftime("%Y-%m-%d %H:%M")
print(formated_date) # prints 2019-12-12 03:42https://stackoverflow.com/questions/62013650
复制相似问题