Python獲取時間的操作示例詳解
更新時間:2022年07月24日 14:29:28 作者:lxw-pro
這篇文章主要為大家詳細介紹了一些Python中獲取時間的操作,例如:獲取時間戳、獲取當前時間、獲取昨天日期等,感興趣的可以參考一下
獲得當前時間時間戳
# 注意時區(qū)的設置
import time
# 獲得當前時間時間戳
now = int(time.time())
# 轉換為其他日期格式,如:"%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
# 獲得當前時間
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))
運行效果如下:

計算每個月天數(shù)
import calendar ???????monthRange = calendar.monthrange(2022, 4) print(monthRange)
計算3天前并轉換為指定格式
import time
import datetime
# 先獲得時間數(shù)組格式的日期
threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days=3))
# 轉換為時間戳
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
# 轉換為其他字符串格式
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)
pandas 每日一練
print()只為換行用,方便看運行結果
# -*- coding = utf-8 -*- # @Time : 2022/7/22 19:46 # @Author : lxw_pro # @File : pandas-5 練習.py # @Software : PyCharm import pandas as pd
21讀取本地EXCEL數(shù)據(jù)
df = pd.read_excel('test-5.xlsx')
print("EXCEL數(shù)據(jù)如下:\n", df)
print()
22查看df數(shù)據(jù)前5行
print("df數(shù)據(jù)前5行為:\n", df.head())
print()
23將popularity列數(shù)據(jù)轉換為最大值與最小值的平均值
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()
24將數(shù)據(jù)根據(jù)project進行分組并計算平均分
fzj = df.groupby('project').mean()
print("分組后的平均分為:\n", fzj)
print()
25將test_time列具體時間拆分為兩部分(一半日期,一半時間)
df['date'] = df['test_time'].dt.date
df['time'] = df['test_time'].dt.time
print(df.head())
df.to_excel('text5.xlsx') # 也可將所運行的結果導入另一個新的EXCEL
相關程序運行結果如下:
21-22:

23-24:
25:

存入的新EXCEL數(shù)據(jù):

到此這篇關于Python獲取時間的操作示例詳解的文章就介紹到這了,更多相關Python獲取時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python學習筆記之open()函數(shù)打開文件路徑報錯問題
這篇文章主要介紹了Python學習筆記之open()函數(shù)打開文件路徑報錯問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
使用OpenCV對車道進行實時檢測的實現(xiàn)示例代碼
這篇文章主要介紹了使用OpenCV對車道進行實時檢測的實現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
python通過ssh-powershell監(jiān)控windows的方法
這篇文章主要介紹了python通過ssh-powershell監(jiān)控windows的方法,涉及Python操作ssh-powershell的相關技巧,需要的朋友可以參考下2015-06-06
探究數(shù)組排序提升Python程序的循環(huán)的運行效率的原因
這篇文章主要介紹了探究數(shù)組排序提升Python程序的循環(huán)的運行效率的原因,作者用代碼實踐了多個小片段來進行對比解釋,需要的朋友可以參考下2015-04-04

