Python 如何优雅的将数字转化为时间格式的方法

yipeiwu_com6年前Python基础

将数字转化成时间格式

from dateutil.parser import parse

a=20170825
b=str(a)
c=parse(b)

print(c)
2017-08-25 00:00:00

将时间按照天排列,转化为一个数字,用来做时间序列分析

from matplotlib.pylab import date2num
 e = date2num(c)

e
Out[30]: 736566.0

将时间转化为时间戳

d=str(c)

d
Out[25]: '2017-08-25 00:00:00'
from matplotlib.pylab import date2num
 
timeArray = time.strptime(d, "%Y-%m-%d %H:%M:%S")

timeArray
Out[27]: time.struct_time(tm_year=2017, tm_mon=8, tm_mday=25, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=237, tm_isdst=-1)

timestamp = time.mktime(timeArray)

timestamp
Out[29]: 1503590400.0

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python 导入文件过程图解

Python 导入文件过程图解

1、同级目录下调用 若在程序 testone.py 中导入模块 testtwo.py , 则直接使用 【import testtwo 或 from testtwo import *】...

python实现2014火车票查询代码分享

代码基于Python3.3.3,PyQt5.1.1 复制代码 代码如下:# -*- coding: utf-8 -*-# Python 3.3.3# PyQt 5.1.1import s...

pyqt5让图片自适应QLabel大小上以及移除已显示的图片方法

pyqt5让图片自适应QLabel大小上以及移除已显示的图片方法

代码: import sys from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QLabel, QApplication) fro...

python实战串口助手_解决8串口多个发送的问题

今晚终于解决了串口发送的问题,更改代码如下: def write(self, data): if self.alive: if self.serSer.isOpe...

mac下如何将python2.7改为python3

mac下如何将python2.7改为python3

1.查看当前电脑python版本 python -V  // 显示2.7.x 2.用brew升级python brew update python  3.如果安装...