• sys模块 json pickle模块


    # sys模块
    # import sys
    # sys.path
    # sys.argv
    # sys.exit() # 脚本退出
    # print('[%s]'%('#'*1))
    # print('[%s]'%('#'*2))
    # print('[%s]'%('#'*3))
    # print('[%s]'%('#'*4))
    # print('[%s]'%('#'*5))
    # print('[%-50s]'%('#'*1))
    # print('[%-50s]'%('#'*2))
    # print('[%-50s]'%('#'*3))
    # print('[%-50s]'%('#'*4))
    # print('[%-50s]'%('#'*5))
    '''
    [#]
    [##]
    [###]
    [####]
    [#####]
    [# ]
    [## ]
    [### ]
    [#### ]
    [##### ]
    '''
    # print('%d%%'%30) # 30%
    # print('[%%-%ds]'%50)
    # print(('[%%-%ds]'%50)%('#'*1))
    # print(('[%%-%ds]'%50)%('#'*2))
    # print(('[%%-%ds]'%50)%('#'*3))
    # print(('[%%-%ds]'%50)%('#'*4))
    # print(('[%%-%ds]'%50)%('#'*5))
    '''
    [%-50s]
    [# ]
    [## ]
    [### ]
    [#### ]
    [##### ]
    '''
    # def progress(percent,width=50):
    # show_str=('[%%-%ds]'%width)%('#'*int(width*percent))
    # print(' %s'%show_str,end='')

    # progress(0.1)
    # progress(0.2)
    # progress(0.3)
    # progress(0.4)
    # progress(0.5)
    '''
    [##### ]
    [########## ]
    [############### ]
    [#################### ]
    [######################### ]
    '''
    # def progress(percent,width=50):
    # show_str=('[%%-%ds]'%width)%('#'*int(width*percent))
    # print(' %s'%show_str,end='')
    # progress(0.1)
    # progress(0.2)
    # progress(0.3)
    # progress(0.4)
    # progress(0.5)
    '''
    [######################### ]
    '''
    # def progress(percent,width=50):
    # show_str=('[%%-%ds]'%width)%('#'*int(width*percent))
    # print(' %s %d%%'%(show_str,int(100*percent)),end='')
    # progress(0.1)
    # progress(0.2)
    # progress(0.3)
    # progress(0.4)
    # progress(0.5)
    '''
    [######################### ] 50%
    '''
    # import time
    # def progress(percent,width=50):
    # show_str=('[%%-%ds]'%width)%('#'*int(width*percent))
    # print(' %s %d%%'%(show_str,int(100*percent)),end='')
    # recv_size=0
    # total_size=198749
    # while recv_size < total_size:
    # time.sleep(0.1)
    # recv_size+=1034
    # progress(recv_size/total_size)
    '''
    [##################################################] 100%
    '''
    # import time
    # def progress(percent,width=50):
    # show_str=('[%%-%ds]'%width)%('#'*int(width*percent))
    # print(' %s %d%%'%(show_str,int(100*percent)),end='')
    # recv_size=0
    # total_size=10241
    # while recv_size < total_size:
    # time.sleep(0.1)
    # recv_size+=1024
    # progress(recv_size/total_size)
    '''
    [######################################################] 109%
    '''
    # import time
    # def progress(percent,width=50):
    # if percent >= 1:
    # percent=1
    # show_str=('[%%-%ds]'%width)%('#'*int(width*percent))
    # print(' %s %d%%'%(show_str,int(100*percent)),end='')
    # recv_size=0
    # total_size=10241
    # while recv_size < total_size:
    # time.sleep(0.1)
    # recv_size+=1024
    # progress(recv_size/total_size)
    '''
    [##################################################] 100%
    '''
    # 序列化可以将内存中的数据结构保存下来,字典 ,列表,元组,字符串...
    # dic={'a':1}
    # with open('db.txt','w',encoding='utf-8')as f:
    # f.write(str(dic))
    # with open('db.txt','r',encoding='utf-8')as f:
    # dic=eval(f.read())
    # print(dic['a'])
    '''
    1
    '''
    '''
    内存中结构化的数据<->格式json<->字符串<->保存到文件中或基于网络传输
    '''
    # eval("[null,false,1]")
    # [null,false,1]
    # import json
    # dic={'a':1}
    # res=json.dumps(dic)
    # print(res,type(res))
    '''
    {"a": 1} <class 'str'>
    '''
    # import json
    # dic={'a':1}
    # res=str(dic)
    # print(res,type(res))
    '''
    {'a': 1} <class 'str'>
    '''
    # import json
    # x=None
    # print(json.dumps(x))
    '''
    null
    '''
    # import json
    # user={'name':'egon','age':18,'nb':True}
    # # with open('user.json','w',encoding='utf-8')as f:
    # # f.write(json.dumps(user))
    # json.dump(user,open('user_new.json','w',encoding='utf-8'))
    # import json,time
    # user={'name':'egon','age':18,'nb':True}
    # with open('user.json','w',encoding='utf-8')as f:
    # f.write(json.dumps(user))
    # students=['egon','wer','axjl']
    # json.dump(students,open('students.json','w',encoding='utf-8'))
    # time.sleep(500)
    '''
    别的文件拿过来的
    import json
    with open('user.json','r',encoding='utf-8')as f:
    user=json.loads(f.read())
    print(user['name']) # egon

    user=json.load(open('user.json','r',encoding='utf-8'))
    print(user['age']) # 18
    print(user['nb']) # True
    '''
    # json_str='{"count":1}'
    # print(json.loads(json_str))
    # print(json.loads(json_str)['count'])
    '''
    {'count': 1}
    1
    '''
    # pickle
    # 可以识别python的所有数据类型
    # 但是不能跨平台
    # import pickle,json
    # s={1,2,3,4}
    # # print(json.dumps(s))
    # print(pickle.dumps(s))
    '''
    b'x80x03cbuiltins set qx00]qx01(Kx01Kx02Kx03Kx04ex85qx02Rqx03.'
    '''
    # with open('s.pkl','wb')as f:
    # f.write(pickle.dumps(s))

    # pickle.dump(s, open('s.pkl','wb'))
    '''
    import pickle

    with open('s.pkl','rb')as f:
    s=pickle.loads(f.read())
    print(s,type(s))

    {1, 2, 3, 4} <class 'set'>
    '''
    '''
    import pickle
    s=pickle.load(open('s.pkl','rb'))
    print(s,type(s))
    {1, 2, 3, 4} <class 'set'>
    '''
  • 相关阅读:
    进程和线程
    VC中使对话框全屏的方法
    初为项目经理
    Sqlite知识小记
    ERROR L105: PUBLIC REFERS TO IGNORED SEGMENT
    RTX51.LIB NOT FOUND
    Virtual Villagers 攻略
    专业术语常用名词缩写中英文对照
    Eric的四个无刷新(异步更新)示例
    ASP.NET 安全认证(一)—— 如何运用 Form 表单认证 .
  • 原文地址:https://www.cnblogs.com/0B0S/p/12040544.html
Copyright © 2020-2023  润新知