溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python獲取時間的代碼怎么寫

發(fā)布時間:2022-07-28 11:00:59 來源:億速云 閱讀:162 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Python獲取時間的代碼怎么寫”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“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))

運行效果如下:

Python獲取時間的代碼怎么寫

計算每個月天數(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()只為換行用,方便看運行結(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進行分組并計算平均分

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')	# 也可將所運行的結(jié)果導(dǎo)入另一個新的EXCEL

相關(guān)程序運行結(jié)果如下:

21-22:

Python獲取時間的代碼怎么寫

23-24:

25:

Python獲取時間的代碼怎么寫

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

Python獲取時間的代碼怎么寫

關(guān)于“Python獲取時間的代碼怎么寫”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細(xì)節(jié)

免責(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)容。

AI