• 常用问题库(2)


    知识点一: datetime模块(时间)

    日期与时间

    import os
    import datetime
    import time
    
    print('-----------日期时间 基本对象----------')
    print(datetime.time(15,45,21,100))  #时间
    print(datetime.date(2018,3,17)) #日期
    print(datetime.datetime(2018,3,17,15,45,21,10001)) #时间+日期

    dl=datetime.datetime.now()  #打印出当前时间
    print(dl)
    du=datetime.datetime.utcnow() #打印出UTC时间
    print(du)
    
    print('-------时间戳转换成日期-----------')
    ds=os.path.getctime('test.py')
    print(datetime.datetime.fromtimestamp(ds))
    print('-------日期转换成时间戳-----------')
    print(time.mktime(dl.timetuple()))

    print('----专门表示时间差的类型------')
    da=datetime.timedelta(hours=2,minutes=30)
    print(datetime.datetime(2018,2,28,23)+da)

    print('-------时间转换成字符串---------')  #实例方法调用
    dt=datetime.datetime(2018,3,17,15,45,21,10001)
    s=dt.strftime('%m/%d/%y %I:%M:%S %p')   #月/日/年  12小时进制
    print(type(s),s)
    print('-------字符串转换成时间---------')  #类方法调用
    sss=datetime.datetime.strptime(s,'%m/%d/%y %I:%M:%S %p')
    print(sss)

    #想要表示市区,得有一个时区对象
    tz_china=datetime.timezone(offset=datetime.timedelta(hours=8),name='Asia/Beijing ')
    tz_us=datetime.timezone(offset=datetime.timedelta(hours=-8),name='Asia/Los Angles ')
    dt =datetime.datetime(2018,3,17,17,16,tzinfo=tz_china)  #这个时间带了时区信息
    
    #现在我dt时区的,想要直接转换成美国时区
    us_dt=dt.astimezone(tz_us)
    print(repr(us_dt),us_dt) 

    知识点二: base64 模块(Web编码)

    base64模块接口

    base64编码,接收一个2二进制的数据,然后把它转换成文本,可以转换回来

    标准编码: base64.standard_b64encode(s)

    标准解码: base64.standard_b64decode(s)

    安全编码: base64.urlsafe_b64encode(s)

    标准编码: base64.urlsafe_b64encode(s)

    实例:

    ttt.py内容:

    import base64
    with open('aaa.png','rb') as f:
        print(base64.standard_b64encode(f.read()))
    
    #base64是将2进制编码成文本
    # 如果是decode,是吧编码成的文本,变成原来的二进制文件
    # 网站有两种方法,一种先打开网址,在加载图片,第二种是直接把图片嵌入编码

    images.html内容:

    <img  src="data:imags/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wgARCABuAG4DASIAAhEBAxEB/8QAGgAAAgMBAQAAAAAAAAAAAAAABQYAAwQBAv/EABYBAQEBAAAAAAAAAAAAAAAAAAABAv/aAAwDAQACEAMQAAABeqrfOcozNprtXi2S0HMObwDDw3eCCerWYiNN0kk8la/kbbaNckkkhMmuAM1ANrBJJIuMajac203yAt43BafG6xYaILRlCIspIya19gXwptqgjLoybgQQB+7T2IEbLdiY4SWSRF9hXWJrii3AEvLr25MOVrqug23bYLxLfnNPBmNMTBWPVh52SLXGYdWaAGKJPeW2/SFoGrIAuShrFmF7OyTnPXkUmpTbjzLYUq7chjzk1WHJ2H//xAAoEAACAgEDAwMEAwAAAAAAAAACAwEEAAUQEhETFCAhIyIxM0EkMjT/2gAIAQEAAQUCxhwtcyc4o58IgM01mfw+LBRXAezZVAsQEdjj9FThzqL7Y7SMFC6y1yurCmOOqJQt7Z8WxzGnYBhrsc61hcwVVZAIQMLUCvQxoJDk++Say0D6HVlviDdTkSgh3mYAfr1KwAwAeohgoRM1LW+os7h1AgRYXAD1dKiG8g1heV0LWkc12WzgMFkZeVLK9Z0WK+yfmfX/ABmPMG1u2mlpPbJ+l1n4mn4mRpncKtVivG2nfG7C9go/4634jPgE3YIVFBDJDGMaY4NtJxE9d1e2t5MdRoe1WtPti6y++1VIzBdWXTcqriUpZcCYkdle+t7RHZurLgX6rAybHd05bQbUMlgXSUSTFK2meMUrChJFoXTmopLAOGLU7jkdJwkrKQWC46bG9KsizL8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EqVKn/2Q==">

    执行结果:

  • 相关阅读:
    eNSP模拟器的路由器无法启动
    Centos7环境下的root账号不能开启VLC
    curl 静态库编译 非https
    Javascript进阶
    区块链 2.0
    Javascript基础
    9 信号量和管程
    Java基础笔记
    Go笔记
    10 死锁
  • 原文地址:https://www.cnblogs.com/taoge188/p/8592885.html
Copyright © 2020-2023  润新知