您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python獲取時間的代碼怎么寫”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“Python獲取時間的代碼怎么寫”文章能幫助大家解決問題。
# 注意時區(qū)的設(shè)置 import time # 獲得當(dāng)前時間時間戳 now = int(time.time()) # 轉(zhuǎn)換為其他日期格式,如:"%Y-%m-%d %H:%M:%S" timeArr = time.localtime(now) other_StyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArr) print(other_StyleTime)
import datetime # 獲得當(dāng)前時間 now = datetime.datetime.now() other_StyleTime = now.strftime("%Y-%m-%d %H:%M:%S") print(other_StyleTime)
import datetime def getYesterday(): today = datetime.date.today() oneday = datetime.timedelta(days=1) yesterday = today - oneday return yesterday print("昨天的日期:", getYesterday())
# 引入日歷模塊 import calendar # 輸入指定年月 yy = int(input("輸入年份:")) mm = int(input("輸入月份:")) # 顯示指定年月 print(calendar.month(yy, mm))
運行效果如下:
import calendar monthRange = calendar.monthrange(2022, 4) print(monthRange)
import time import datetime # 先獲得時間數(shù)組格式的日期 threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days=3)) # 轉(zhuǎn)換為時間戳 timeStamp = int(time.mktime(threeDayAgo.timetuple())) # 轉(zhuǎn)換為其他字符串格式 otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S") print(otherStyleTime)
import time import datetime # 給定時間戳 timeStamp1 = 1643892140 dateArray = datetime.datetime.utcfromtimestamp(timeStamp1) threeDayAgo = dateArray - datetime.timedelta(days=3) print(threeDayAgo)
import time timeStamp = 1825135462 timeArr = time.localtime(timeStamp) other_StyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArr) print(other_StyleTime)
或
import datetime timeStamp = 2022020321 dateArr = datetime.datetime.utcfromtimestamp(timeStamp) other_StyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S") print(other_StyleTime)
print()只為換行用,方便看運行結(jié)果
# -*- coding = utf-8 -*- # @Time : 2022/7/22 19:46 # @Author : lxw_pro # @File : pandas-5 練習(xí).py # @Software : PyCharm import pandas as pd
df = pd.read_excel('test-5.xlsx') print("EXCEL數(shù)據(jù)如下:\n", df) print()
print("df數(shù)據(jù)前5行為:\n", df.head()) print()
import re def func(df): zfg = df['popularity'].split('-') smin = int(zfg[0].strip('f')) smax = int(zfg[1].strip('f')) df['popularity'] = int((smin+smax)/2) return df df = df.apply(func, axis=1) print(df) print()
fzj = df.groupby('project').mean() print("分組后的平均分為:\n", fzj) print()
df['date'] = df['test_time'].dt.date df['time'] = df['test_time'].dt.time print(df.head()) df.to_excel('text5.xlsx') # 也可將所運行的結(jié)果導(dǎo)入另一個新的EXCEL
相關(guān)程序運行結(jié)果如下:
21-22:
23-24:
25:
存入的新EXCEL數(shù)據(jù):
關(guān)于“Python獲取時間的代碼怎么寫”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。