Python獲取時間的操作示例詳解
獲得當(dāng)前時間時間戳
# 注意時區(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)
獲取當(dāng)前時間
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))
運(yùn)行效果如下:
計算每個月天數(shù)
import calendar ???????monthRange = calendar.monthrange(2022, 4) print(monthRange)
計算3天前并轉(zhuǎn)換為指定格式
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)
pandas 每日一練
print()只為換行用,方便看運(yùn)行結(jié)果
# -*- coding = utf-8 -*- # @Time : 2022/7/22 19:46 # @Author : lxw_pro # @File : pandas-5 練習(xí).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ù)轉(zhuǎn)換為最大值與最小值的平均值
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進(jìn)行分組并計算平均分
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') # 也可將所運(yùn)行的結(jié)果導(dǎo)入另一個新的EXCEL
相關(guān)程序運(yùn)行結(jié)果如下:
21-22:
23-24:
25:
存入的新EXCEL數(shù)據(jù):
到此這篇關(guān)于Python獲取時間的操作示例詳解的文章就介紹到這了,更多相關(guān)Python獲取時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python學(xué)習(xí)筆記之open()函數(shù)打開文件路徑報錯問題
這篇文章主要介紹了Python學(xué)習(xí)筆記之open()函數(shù)打開文件路徑報錯問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04使用OpenCV對車道進(jìn)行實時檢測的實現(xiàn)示例代碼
這篇文章主要介紹了使用OpenCV對車道進(jìn)行實時檢測的實現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06python通過ssh-powershell監(jiān)控windows的方法
這篇文章主要介紹了python通過ssh-powershell監(jiān)控windows的方法,涉及Python操作ssh-powershell的相關(guān)技巧,需要的朋友可以參考下2015-06-06探究數(shù)組排序提升Python程序的循環(huán)的運(yùn)行效率的原因
這篇文章主要介紹了探究數(shù)組排序提升Python程序的循環(huán)的運(yùn)行效率的原因,作者用代碼實踐了多個小片段來進(jìn)行對比解釋,需要的朋友可以參考下2015-04-04python面向?qū)ο蠖嗑€程爬蟲爬取搜狐頁面的實例代碼
這篇文章主要介紹了python面向?qū)ο蠖嗑€程爬蟲爬取搜狐頁面的實例代碼,需要的朋友可以參考下2018-05-05Python函數(shù)默認(rèn)參數(shù)設(shè)置的具體方法
本文主要介紹了Python函數(shù)默認(rèn)參數(shù)設(shè)置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03PyQt5 QTableView設(shè)置某一列不可編輯的方法
今天小編就為大家分享一篇PyQt5 QTableView設(shè)置某一列不可編輯的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06