zoukankan      html  css  js  c++  java
  • Python模块——base64

    base64模块是用来作base64编码解码,常用于小型数据的传输。编码后的数据是一个字符串,其包括a-z、A-Z、0-9、/、+共64个字符,即可用6个字节表示,写出数值就是0-63.故三个字节编码的话就变成了4个字节,如果数据字节数不是3的倍数,就不能精确地划分6位的块,此时需要在原数据后添加1个或2个零值字节,使其字节数为3的倍数,然后在编码后的字符串后添加1个或2个‘=’,表示零值字节,故事实上总共由65个字符组成.

     base64模块真正用的上的方法只有8个,分别是encode, decode, encodestring, decodestring, b64encode,b64decode, urlsafe_b64decode,urlsafe_b64encode。他们8个可以两两分为4组,encode,decode一组,专门用来编码和解码文件的,也可以StringIO里的数据做编解码;encodestring,decodestring一组,专门用来编码和解码字符串; b64encode和b64decode一组,用来编码和解码字符串,并且有一个替换符号字符的功能;urlsafe_b64encode和urlsafe_b64decode一组,这个就是用来专门对url进行base64编解码的。


    import base64
    #st = 'hello world'.encode()

    #print(st) #b'hello world'
    '''
    st = 'hello world'.encode('utf-8')
    print(st)
    b'hello world'

    '''
    st = 'hello world'.encode()
    #print(st.decode())#hello world
    b = base64.b64encode(st)
    #print(b) # b'aGVsbG8gd29ybGQ='
    print(b.decode())#aGVsbG8gd29ybGQ=
    b = base64.b64decode(b)
    print(b.decode())#hello world

  • 相关阅读:
    键盘输入thisisunsafe
    vscode
    iterm2 rz sz
    homebrew镜像更换
    mac
    homebrew下载不成功
    shutil:高层文件操作
    tempfile:临时文件系统对象
    linecache:读取文本文件的指定内容
    fnmatch:Unix式glob模式匹配,简单场景下可以代替正则
  • 原文地址:https://www.cnblogs.com/tingtin/p/12592050.html
Copyright © 2011-2022 走看看