在 Python 中生成 ISO8601 格式的随机日期时间列表
Generate list of random datetimes in ISO8601 format in Python
我想生成一个长度为 n 的列表,其中包含 Python 范围内的随机日期时间字符串。
import datetime
start = datetime.datetime(2019,1,1,0,0,0).astimezone().replace(microsecond=0).isoformat()
end = datetime.datetime(2019,12,31,23,59,59).astimezone().replace(microsecond=0).isoformat()
I had asked a similar question before,但是我没有使用正确的格式 datetime.What 是最好的实现方式吗?
在此处根据您的格式修改答案:
from random import randrange
from datetime import timedelta, datetime
start = datetime(2019,1,1,0,0,0).astimezone().replace(microsecond=0)
end = datetime(2019,12,31,23,59,59).astimezone().replace(microsecond=0)
n = 5
def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
"""
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = randrange(int_delta)
return start + timedelta(seconds=random_second)
random_dates = [random_date(start, end) for i in range(n)]
输出:
[datetime.datetime(2019, 8, 6, 2, 25, 32, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time')), datetime.datetime(2019, 8, 5, 13, 2, 15, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time')), datetime.datetime(2019, 6, 24, 15, 57, 19, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time')), datetime.datetime(2019, 5, 3, 9, 35, 8, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time')), datetime.datetime(2019, 2, 12, 14, 33, 35, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time'))]
然后,如果您想要文本 ISO 格式,只需在您的列表元素上调用 .isoformat()
。
[random_date(start, end).isoformat() for i in range(n)]
#['2019-05-03T22:06:56+11:00', '2019-12-31T06:56:09+11:00', '2019-03-09T05:07:59+11:00', '2019-03-24T09:24:42+11:00', '2019-01-08T03:23:31+11:00']
我想生成一个长度为 n 的列表,其中包含 Python 范围内的随机日期时间字符串。
import datetime
start = datetime.datetime(2019,1,1,0,0,0).astimezone().replace(microsecond=0).isoformat()
end = datetime.datetime(2019,12,31,23,59,59).astimezone().replace(microsecond=0).isoformat()
I had asked a similar question before,但是我没有使用正确的格式 datetime.What 是最好的实现方式吗?
在此处根据您的格式修改答案:
from random import randrange
from datetime import timedelta, datetime
start = datetime(2019,1,1,0,0,0).astimezone().replace(microsecond=0)
end = datetime(2019,12,31,23,59,59).astimezone().replace(microsecond=0)
n = 5
def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
"""
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = randrange(int_delta)
return start + timedelta(seconds=random_second)
random_dates = [random_date(start, end) for i in range(n)]
输出:
[datetime.datetime(2019, 8, 6, 2, 25, 32, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time')), datetime.datetime(2019, 8, 5, 13, 2, 15, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time')), datetime.datetime(2019, 6, 24, 15, 57, 19, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time')), datetime.datetime(2019, 5, 3, 9, 35, 8, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time')), datetime.datetime(2019, 2, 12, 14, 33, 35, tzinfo=datetime.timezone(datetime.timedelta(seconds=39600), 'AUS Eastern Summer Time'))]
然后,如果您想要文本 ISO 格式,只需在您的列表元素上调用 .isoformat()
。
[random_date(start, end).isoformat() for i in range(n)]
#['2019-05-03T22:06:56+11:00', '2019-12-31T06:56:09+11:00', '2019-03-09T05:07:59+11:00', '2019-03-24T09:24:42+11:00', '2019-01-08T03:23:31+11:00']