python自动生成一年的时间(精确到秒)
程序员文章站
2022-05-18 19:17:39
import timeimport randomimport pandas as pdbegin_date = (2019, 9, 1, 0, 0, 0, 0, 0, 0) #設置開始日期時間元組(2019-09-01 00:00:00)end_date = (2020, 8, 31, 23, 59, 59, 0, 0, 0) #設置結束日期時間元組(2020-08-31 23:59:59)start = time.mktime(begin_date) #生成開始時間end = time.m...
import time
import random
import pandas as pd
begin_date = (2019, 9, 1, 0, 0, 0, 0, 0, 0) #設置開始日期時間元組(2019-09-01 00:00:00)
end_date = (2020, 8, 31, 23, 59, 59, 0, 0, 0) #設置結束日期時間元組(2020-08-31 23:59:59)
start = time.mktime(begin_date) #生成開始時間
end = time.mktime(end_date) #生成結束時間
list_data=[]
for i in range(178030):
random_date = random.randint(start, end) #在開始和結束時間戳中隨機取出一個
transform_date = time.localtime(random_date) #將時間戳生成時間元組
date = time.strftime("%Y-%m-%d %H:%M:%S", transform_date) #將時間元組轉成格式化字符串
#print(date)
list_data.append(date)
#print(list_data)
df=pd.DataFrame(list_data)
df=df.sort_values(by=0).reset_index()
df.to_csv('C:/Users/mingming/Desktop/比赛/ancewer.csv',index=False,sep=',')
本文地址:https://blog.csdn.net/qq_42888622/article/details/107442594