1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| """ sys 主要用于获取python解释器相关信息 """ import sys
print("-----------sys----------------") print([m for m in dir(sys) if not m.startswith("_")])
print(sys.byteorder) print(sys.copyright) print(sys.executable) print(sys.getfilesystemencoding()) print(sys.maxsize) print(sys.platform) print(sys.version) print(sys.winver)
print("len(sys.argv):", len(sys.argv)) print("sys.argv[0]:", sys.argv[0])
print(sys.path) sys.path.append('F:\\Game') print(sys.path)
""" os 模块获取程序所在的操作系统相关信息 """ import os
print('---------os----------') print([e for e in dir(os) if not e.startswith("_")])
print(os.name) print(os.getenv('PYTHONPATH')) print(os.getlogin()) print(os.getpid()) print(os.getppid()) print(os.cpu_count()) print("os.sep:", os.sep) print("os.pathsep:", os.pathsep) print("os.linesep", os.linesep) print(os.urandom(3))
""" random 模块 主要生成伪随机数 """ import random
print("----------random---------") print(random.__all__) print(random.random()) print(random.uniform(2.5, 10.0)) print(random.expovariate(1 / 5)) print(random.randrange(10)) print(random.randrange(0, 101, 2)) print(random.choice(['java', 'go', 'python'])) lang = ['java', 'go', 'python'] random.shuffle(lang) print(lang)
print(random.sample(['1', '2', '3', '4', '5', '6', '7'], 3))
""" time 模块 主要提供日期、时间功能 """ import time
print('-----------time----------') print([e for e in dir(time) if not e.startswith("_")]) print(time.asctime()) print(time.asctime((2018, 3, 20, 8, 10, 12, 0, 0, 0))) print(time.ctime(30)) print(time.gmtime(30)) print(time.gmtime()) print(time.localtime(30)) print(time.mktime((2018, 3, 20, 8, 10, 12, 0, 0, 0))) print(time.perf_counter()) print(time.process_time()) time.sleep(1) print(time.strftime('%Y-%m-%d %H:%M:%S')) st = '2019-03-11 11:12:10' print(time.strptime(st, '%Y-%m-%d %H:%M:%S')) print(time.time()) print(time.timezone)
""" JSON 转换关系 python dict--> JSON object python list,tuple-> json array ... JSON object --> python dict json array -> python list ... """ import json
print("----------json----------") print(json.__all__) s = json.dumps(['ha', {'favorite': ('coding', None, 24, 'game')}]) print(s) s2 = json.dumps('hello world') print(s2) s3 = json.dumps({'c': 2, 'a': '22', 'b': 're'}, sort_keys=True, separators=(',', ':')) print(s3) s4 = json.dumps({'py': 5, 'go': 2, 'java': (4, 7)}, indent=4) print(s4) s5 = json.JSONEncoder().encode({'names': ('你好', '静静')}) print(s5) f = open('a.json', 'w') json.dump(['go', {'java': 'e'}], f)
r1 = json.loads('["ha",{"fa":["code",null,24,"games"]}]') print(r1) r2 = json.loads('"for\\"poo"') print(r2)
def full_name(dct): if '__f__' in dct: return dct['f_name'] + dct['l_name'] return dct
r3 = json.loads('{"__f__":true,"f_name":"xiao","l_name":"wang"}', object_hook=full_name) print(r3) f2 = open('b.json') r4 = json.load(f2) print(r4)
class ComplexEncoder(json.JSONEncoder):
def default(self, o): if isinstance(o, complex): return {"__complex__": "true", 'real': o.real, 'imag': o.imag} return json.JSONEncoder.default(self, o)
c1 = json.dumps(2 + 1j, cls=ComplexEncoder) print(c1) c2 = ComplexEncoder().encode(2 + 1j) print(c2)
|