網站首頁 編程語言 正文
獲得當前時間時間戳
# 注意時區的設置
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))
運行效果如下:
計算每個月天數
import calendar
???????monthRange = calendar.monthrange(2022, 4)
print(monthRange)
計算3天前并轉換為指定格式
import time
import datetime
# 先獲得時間數組格式的日期
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數據
df = pd.read_excel('test-5.xlsx')
print("EXCEL數據如下:\n", df)
print()
22查看df數據前5行
print("df數據前5行為:\n", df.head())
print()
23將popularity列數據轉換為最大值與最小值的平均值
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將數據根據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數據:
原文鏈接:https://blog.csdn.net/m0_66318554/article/details/125685638
相關推薦
- 2022-11-09 GO?語言運行環境的基礎知識_Golang
- 2022-03-10 通過容器擴展屬性IExtenderProvider實現WinForm通用數據驗證組件_C#教程
- 2022-09-17 C++?中如何結束?while?(cin>>str)?的輸入_C 語言
- 2022-12-04 如何對csv文件數據分組,并用pyecharts展示_python
- 2022-04-15 golang數組和切片作為參數和返回值的實現_Golang
- 2022-06-08 報錯:No fallback instance of type class**解決辦法
- 2022-08-30 c語言學習——動態內存分配
- 2022-10-14 Springboot整合Netty提供WebSocket服務
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支