欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python獲取時間的操作示例詳解

 更新時間:2022年07月24日 14:29:28   作者:lxw-pro  
這篇文章主要為大家詳細(xì)介紹了一些Python中獲取時間的操作,例如:獲取時間戳、獲取當(dāng)前時間、獲取昨天日期等,感興趣的可以參考一下

獲得當(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)文章

最新評論