编码转换测试

# -*-coding:utf8-*-

import codecs
from urllib import parse
import base64
import ast

def test():
    print('中国China123')  # 中国China123

    # str --encode-->unocodeBytes
    print('中国China123'.encode('unicode_escape'))  # b'\\u4e2d\\u56fdChina123'
    # unocodeBytes --decode--> str
    print(b'\\u4e2d\\u56fdChina123'.decode('unicode_escape')) # 中国China123

    # str --encode--> utf8Bytes
    print('中国China123'.encode('utf-8'))  # b'\xe4\xb8\xad\xe5\x9b\xbdChina123'
    print(bytes('中国China123', 'utf-8'))  # b'\xe4\xb8\xad\xe5\x9b\xbdChina123'
    # utf8Bytes --decode--> str
    print(b'\xe4\xb8\xad\xe5\x9b\xbdChina123'.decode('utf-8'))  # 中国China123
    print(str(b'\xe4\xb8\xad\xe5\x9b\xbdChina123', 'utf-8'))  # 中国China123
    
    # str --encode--> gbkBytes
    print('中国China123'.encode('gbk'))  # b'\xd6\xd0\xb9\xfaChina123'
    print(bytes('中国China123', 'gbk'))
    # gbkBytes --decode--> str
    print(b'\xd6\xd0\xb9\xfaChina123'.decode('gbk'))  # 中国China123
    print(str(b'\xd6\xd0\xb9\xfaChina123', 'gbk'))  # 中国China123

    # hexStr --> bytes
    print(bytes.fromhex('aabbcc112233')) # b'\xaa\xbb\xcc\x11"3'
    # bytes --> hexStr
    print(b'\xaa\xbb\xcc\x11"3'.hex())  # aabbcc112233
    print(''.join(['%02x' % b for b in b'\xaa\xbb\xcc\x11"3']))  # aabbcc112233
    
    # bytes --encode--> hexBytes
    print(codecs.encode(b'\xaa\xbb\xcc\x11"3', 'hex')) # b'aabbcc112233'
    # hexBytes --decode--> bytes
    print(codecs.decode(b'aabbcc112233', 'hex'))  # b'\xaa\xbb\xcc\x11"3'

    utf8Str = '中国China123'.encode('utf-8') # b'\xe4\xb8\xad\xe5\x9b\xbdChina123'
    # bytes --encode--> base64Bytes
    print(base64.b64encode(utf8Str)) # b'5Lit5Zu9Q2hpbmExMjM='
    # base64Bytes --decode--> bytes
    print(base64.b64decode(b'5Lit5Zu9Q2hpbmExMjM=')) # b'\xe4\xb8\xad\xe5\x9b\xbdChina123'    

    # urlencode
    print(parse.quote('?中国&China=123')) # %3F%E4%B8%AD%E5%9B%BD%26China%3D123
    # urldecode
    print(parse.unquote('%3F%E4%B8%AD%E5%9B%BD%26China%3D123')) # ?中国&China=123
    # urlencode dict
    data = {
        'ch': '中国',
        'en': 'China'
    }
    print(parse.urlencode(data))  # ch=%E4%B8%AD%E5%9B%BD&en=China

    # char --> ascii
    print(ord('a'), ord('A'), ord('1'), ord('&'), ord('\t'), ord(' '), ord('\n')) # 97 65 49 38 9 32 10
    print(chr(97), chr(65), chr(49), chr(38), chr(9), chr(32), chr(10)) # a A 1 &     

    # print(ast.literal_eval("b'\xe4\xb8\xad\xe5\x9b\xbdChina123'")) # SyntaxError: bytes can only contain ASCII literal characters.
    print(list("b'\xe4\xb8\xad\xe5\x9b\xbdChina123'"))
    print(ast.literal_eval(r"b'\xe4\xb8\xad\xe5\x9b\xbdChina123'")) # b'\xe4\xb8\xad\xe5\x9b\xbdChina123'
    print(list(r"b'\xe4\xb8\xad\xe5\x9b\xbdChina123'"))

    bb = b'\\u4e2d\\u56fdChina123'
    with open('tmp.txt', 'w') as fp:
        fp.write(str(bb))
    with open('tmp.txt', 'r') as fp:
        ss = fp.readline()
    assert ast.literal_eval(ss) == bb
    # link: https://stackoverflow.com/questions/59777657/how-to-convert-python-bytes-string-representation-to-bytes

    with open('qpxcBooking_req.json', 'r') as fp:
        aa = fp.readline()
    aa = ast.literal_eval(aa)
    aa = aa.decode('utf-8')
    print(aa)

def test2():
    # s = b'0001mV3wdZnyRbGTgLmzpHbBvvJfjyeJz26+vtZDmM+TbYYAoqCO/WXxwa8un7BE/+sii5bBG/neKShoPRTQw0qfgMyXaN/anYmOJdBT3IKCoSkR/DKhR4aAZQxXIu72aodFW6/nys71okfb3zpANh6RLr/KJ9NNLHYT2dB+oO57c6iVfNV8C8968QHgPzYky5PJPvptOG3IIFDIPeT/G7Nuw/zVdmLJd0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NGRB0ZS7gP615uxejR7UMnxf42BxpwKyz44auMIidFmL9q3T3EiZmH/ITiA8CvW7w2KFFvzDA0BCv6zz5S45H20xPIofy1UZzfjrvYPw9zy/HhgGJYPIfrWLymUbjyrTzaK+BOosMwK1BpbJiNE6ivJEMNcyawdgEpJzHtJ2Wp91Ege+87BswR3KrR53zaZA4qANW1SMNEpFVCPM6Esg/26LUvJ+viF6kY4b3SOVBaEcln60FtgqsPNVTzhKgB4gOJuN81S3OJoZ4StZ52+l2vyD6oEblzqtuxmi/W4LKsbjyFhf56PizCCS9o8H58Dz2qr24YR27EyijoGOj5lZXyApu02RbOBnn5KnZO6HmNhmd4d+yN9cOZ4OL7hyXlEDD4xGh01F6fOYSUSJEcXYwONkl+AfYiRUjCXWTMs8v7qrCo+ndgbnzJBWdjfEa3rW4h8OImu5t5qFTSUYgJJaoiWn6zbkGm3soQ6rJHWSCEXAWw38+p+wNkBgxB2earUo9A+Pp48W2+mw+jGBAWuwERFnvtoq3YIo5001htwJdEKTSZipod23KuUwZ7eAzvn4CnbSclLdx/OxLmzrAFaIAJN8Rzt4sqycLcg6xu3NwgDFBcuMcV1CTYGYyZT8fvtDrvhgD1NjV1Je8SjvQ5f0GH+RROHibu/NUaitpVfulAs2VHPgRtgRjBUSePVu57d6H07bXubpBpk8nlBVunFDL1G/gkUexKt1b+udGbEeU5+94ehCCtcCmDbYfFLsr8l754bFAt76C8YD6Rd1x41KAWSjH2/0N8ArCDa21xRZq4NAqCOY38kvoQQdYwqgndv78k8N8M7yL+YL4L+Vfm2dHaRa1BkrD1T2NzBbS3tQpBk2g=='
    # print(base64.b64decode(s).decode('utf-8'))

    with open('aa', 'r') as fp:
        s = fp.readline()
    print(ast.literal_eval(s))

    # s = b'GIF89a\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;'
    # with open('a.gif', 'wb') as fp:
    #     fp.write(s)

    # with open('agif', 'r') as fp:
    #     s = fp.readline()
    # s = ast.literal_eval(s)
    # with open('a.gif', 'wb') as fp:
    #     fp.write(s)

if __name__ == '__main__':
    # test()
    test2()