1===============json
import json
list = [1, 2, 'abc', {'key': 456}]
print(json.dumps(list))
dumps 把pthon变成json
data = '[123, 456, "abc", {"key": 234}]'
result = json.loads(data)
print(result[2])
loads把json变成python
2==================random
import random
print(random.random())
print(random.randint(10, 100))
print(random.choice(['abc', 'bcd', 'cde']))
0.23595889256900648
77
cde
3===================datetime
import datetime
print(datetime.datetime.now())
now = datetime.datetime.now()
print(now - datetime.timedelta(days= -1, hours=4))
print(now.month)
2019-09-26 22:17:46.572562
2019-09-27 18:17:46.572562
9
import time
data = time.localtime(time.time())
print(time.strftime("%Y-%m-%d %H:%M:%S"), data)
string = "2018-07-02 12:59:23"
print(time.strptime(string, "%Y-%m-%d %H:%M:%S"))
2019-09-26 22:26:35 time.struct_time(tm_year=2019, tm_mon=9, tm_mday=26, tm_hour=22, tm_min=26, tm_sec=35, tm_wday=3, tm_yday=269, tm_isdst=0)
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=2, tm_hour=12, tm_min=59, tm_sec=23, tm_wday=0, tm_yday=183, tm_isdst=-1)
4==============ConfigParser ini配置文件
#ini配置文件 from configparser import ConfigParser parser = ConfigParser() parser.read("text.ini") print(parser.sections()) for section in parser.sections(): print(parser.options(section)) print(parser.get("seg1", "key1"))
ini配置文件按照节的方式存储text.ini
[seg1] key1 = value1 key2 = value2 [seg2] key3 = value3 key4 = value4
5、===========hashlib
import hashlib hash = hashlib.md5() hash.update('snail'.encode('utf-8')) print(hash.hexdigest())