标签:string creat sample case with open tuple 字符串 edit mktime
t = time.localtime(time.time()-86400) # 返回本地时间 print(t.tm_year, t.tm_mon) # 时间戳 time.time() # 格式化前一天时间 t = time.localtime(time.time()-86400) time.strftime(‘%Y/%m/%d %H:%M:%S %W‘, t) # 日期字符串转换成时间对象 time_str = ‘2019-11-12 11:11:11‘ f = time.strptime(time_str, ‘%Y-%m-%d %H:%M:%S‘) print(f) # time.struct_time(tm_year=2019, tm_mon=11, tm_mday=12, tm_hour=11, tm_min=11, tm_sec=11, tm_wday=1, tm_yday=316, tm_isdst=-1) # 将时间对象转换成时间戳 print(time.mktime(f)) # 将时间戳转为字符串格式 print(time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time())))
import datetime print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925 print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19 print(datetime.datetime.now() ) print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天 print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分 datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 时间对象与字符串互转 datetime.datetime.fromtimestamp(141456567) 时间对象与时间戳互转 time.mktime(datetime.datetime.now().timetuple()) 转换成秒
import random print (‘小于1的随机浮点数:‘, random.random()) print (‘指定范围内的随机浮点数:‘, random.uniform(20,30)) print (‘指定范围内的随机整数:‘, random.randint(1,2)) print (‘按指定步长集合中获取一个随机数‘, random.randrange(1,10,3)) # 步长 print (random.randrange(1,10)) # 不包含10 print(‘获取指定长度的序列: ‘, random.sample([1,2,3,4,5],3)) # 返回随机列表
接收多个序列作为参数,返回一个元组列表
x = [1, 2, 3] y = [4, 5, 6,444] z = [7, 8, 9] xyz = zip(x, y, z) list(xyz) # [(1, 4, 7), (2, 5, 8), (3, 6, 9)] x = [1, 2, 3] r = zip(* [x] * 3) list(r) # [(1, 1, 1), (2, 2, 2), (3, 3, 3)]
import string print(‘小写字母: ‘,string.ascii_lowercase) print(‘大写字母: ‘, string.ascii_uppercase) print(‘大写和小写字母: ‘, string.ascii_letters) print(‘数字: ‘, string.digits) print(‘所有符号: ‘, string.printable)
有如下对象
data = { ‘id‘: 1234, ‘credit‘: 15000, ‘balance‘: 8000, ‘create_date‘: ‘2015-04-04‘ }
t = pickle.dumps(data) # data type is obj t2 = pickle.loads(t) # t type is str print(‘t: ‘, type(t),t) print(‘t2: ‘, type(t),t2) 返回: t: <class ‘bytes‘> b‘\x80\x03}q\x00(X\x06\x00\x00\x00creditq\x01M\x98:X\x07\x00\x00\x00balanceq\x02M@\x1fX\x0b\x00\x00\x00create_dateq\x03X\n\x00\x00\x002015-04-04q\x04X\x02\x00\x00\x00idq\x05M\xd2\x04u.‘ t2: <class ‘bytes‘> {‘credit‘: 15000, ‘balance‘: 8000, ‘create_date‘: ‘2015-04-04‘, ‘id‘: 1234}
with open(‘test‘, ‘wb‘) as f: pickle.dump(data, f) # pickle对象(字符串)-->python对象 从文件读取 with open(‘test‘, ‘rb‘) as f: ret = pickle.load(f) print(‘pickle.load: ‘, ret)
标签:string creat sample case with open tuple 字符串 edit mktime
原文地址:http://www.cnblogs.com/hanqian/p/7677762.html