python 第三方库 xToolkit库工具使用详细教程香不香

1 什么是xToolkit库
库xToolkit的中文名字叫X工具集.是python内置库的一个扩展库.把python的datetime,string,list,dist,xthread等数据结构进行了系统库功能的扩展。

安装方法(利用阿里云的pypi源安装会比默认的pypi快很多):

pip install xToolkit -i https://mirrors.aliyun.com/pypi/simple/

备用安装方法(由于国内几个源同步pypi源的频率都不同,如果这个频率慢了,就换其他的):

# 豆瓣源
pip install xToolkit  -i  http://pypi.douban.com/simple --trusted-host pypi.douban.com

升级方法(利用阿里云的pypi源安装会比默认的pypi快很)

pip install --upgrade xToolkit -i https://mirrors.aliyun.com/pypi/simple/

导入方法:

from xToolkit import xstring, xdatetime, xthreading, xlise, xfile

本库使用到了第三库 python-dateutil,jieba,numpy,pandas,emoji,在安装本库的时候会自动安装,不过其中有几个库比较大,可以提前安装好,这样可以避免安装第三方库的时候安装出错。

2 适用对象
适用对象:python工程师
作者:熊利宏
邮箱:xionglihong@163.com
有任何意见欢迎发送邮件,我们一起打造一个好用的python内置库的扩展库
操作文档CSDN地址:https://blog.csdn.net/qq_22409661/article/details/108531485
3 怎么使用xToolkit呢?
3.1 时间模块 xdatetime模块
3.1.1 判断时间格式时分是否正确
支持判断的类型包括 date,datetime,time,int,float,str 其他类型默认为False

xdatetime.shape("2020-03-23 00:00:00")
>> True

xdatetime.shape(25251425)
>> True

xdatetime.shape(253698.25)
>> True

xdatetime.shape("2020-03-")
>> False

xdatetime.shape("english")
>> False

xdatetime.shape("我是一个兵")
>> False

xdatetime.shape("258741")
>> True

xdatetime.shape("2020/03/20T10:09:06.252525+0800")
>> True

xdatetime.shape(datetime.datetime(2020, 9, 29, 8, 12))
>> True

xdatetime.shape(datetime.date(2020, 9, 29))
>> True

xdatetime.shape(datetime.time(8, 9, 29))
>> True

3.1.2 get方法创建对象
利用get方法可以创建时间对象,并且创建的方法还比较多,可以传入时间戳,时间字符串,datetime对象,date对象等

# 时间戳方法
xdatetime.get(98787987)
>> 1973-02-17T17:06:27+08:00

#字符串方式
xdatetime.get("1988-07-20")
>> 1988-07-20T00:00:00

xdatetime.get("2020-09-22-10-06-14")
>> 2020-09-22T10:06:14

# datetime对象
xdatetime.get((datetime(2020, 3, 23, 21, 56, 12))
>> 2020-03-23T21:56:12

# date对象等
xdatetime.get(date(2020, 3, 23))
>> 2020-03-23T00:00:00

3.1.3 获取时间戳
获取当前时间戳

# 此方法获取的时间戳没有微妙部分,如果需要获取微妙部分,用time.time()
xdatetime.get().timestamp
>> 1585833834.0

获取指定时间的时间戳

xdatetime.get("2020-04-02 21:26:54").timestamp
>> 1585834014.0

3.1.4 获取年月日时分秒
获取日期时间元素,年月日时分秒,微妙

# 年
xdatetime.get().year
>> 2020
1
2
3
# 月
xdatetime.get().month
>> 4

# 日
xdatetime.get().day
>> 2

# 时
xdatetime.get().hour
>> 21

# 分
xdatetime.get().minute
>> 37

# 秒
xdatetime.get().second
>> 48

# 微妙
xdatetime.get().microsecond
>> 70815

# 星期
xdatetime.get().weekday
# 返回数字 1-7代表周一到周日
>> 5

# 周
xdatetime.get().weed
# 返回整数代表当前是本年第多少个周
>> 35

# 季
xdatetime.get().quarter
# 返回整数代表当前是本年第几个季度
>> 3

3.1.5 时间推移
shift方法获取某个时间之前或之后的时间,关键字参数:years, months, days, hours,minutes,seconds,microseconds, weeks

# 一年以前
xdatetime.get().shift(years=-1)
>> 2019-04-03T21:10:49.095790+08:00

# 一年以后
xdatetime.get().shift(years=1)
>> 2021-04-03T21:10:49.095790+08:00

#一个月之后
xdatetime.get().shift(months=1)
>> 2020-05-03T21:12:17.332790+08:00

#一天以后
xdatetime.get().shift(days=1)
>> 2020-04-04T21:14:30.914443+08:00

#一个小时以后
xdatetime.get().shift(hours=1)
>> 2020-04-03T22:14:08.301192+08:00

#一分钟以后
xdatetime.get().shift(minutes=1)
>> 2020-04-03T21:17:27.956196+08:00

#一秒钟以后
xdatetime.get().shift(seconds=1)
>> 2020-04-03T21:16:45.380686+08:00

#一毫秒以后
xdatetime.get().shift(microseconds=1)
>> 2020-04-03T21:16:58.252929+08:00

#一周以后
xdatetime.get().shift(weeks=1)
>> 2020-04-10T21:17:11.827210+08:00

3.1.6 时间替换
替换datetime对象,年月日时分秒某一部分,返回一个被替换后的datetime对象,原对象不变关键字参数:year, month, day, hour,minute,second,microsecond

# 把年替换会成2018
xdatetime.get().replace(year=2018)
>> 2018-04-03T21:23:42.819295+08:00

# 把月替换会成10
xdatetime.get().replace(month=10)
>> 2018-10-03T21:23:42.819295+08:00

# 把日替换会成7
xdatetime.get().replace(day=7)
>> 2018-04-07T21:23:42.819295+08:00

# 把时替换会成22
xdatetime.get().replace(hour=22)
>> 2018-04-03T22:23:42.819295+08:00

# 把分替换会成21
xdatetime.get().replace(minute=21)
>> 2018-04-03T21:21:42.819295+08:00

# 把秒替换会成21
xdatetime.get().replace(second=21)
>> 2018-04-03T21:23:21.819295+08:00

3.1.7 时间扩展部分
3.1.7.1 二个时间的差值
计算二个时间的差值,返回值为秒数,传入的二个时间格式包括,时间字符串,datetime,时间戳等

xdatetime.get("2020-04-28 10:52:52", "1988-07-20 17:31:12").how
>> 1002648100

xdatetime.get("2020-04-28", "1988-07-20 17:31:12").how
>> 1002608928

xdatetime.get("1975-04-28 14:14:55", "1988-07-20 17:31:12").how
>> -417496577

3.1.7.2 开始与结束时间
返回 指定时间中,年,月,周的开始时间和结束时间
类型genre Y->年,M->月,W->周
第一个参数:年
第二个参数:年月类型中,代表月,周类型代表周数
 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值