data & timedate & random

复制代码
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author:woshinidaye
 4 
 5 #处理时间的模块:time&datetime
 6 import time,datetime
 7 #1、时间戳
 8 #time() -> floating point number。Return the current time in seconds since the Epoch.
 9 print(time.time())
10 
11 #2、格式化字符串
12 
13 #3、元组
14 print(time.localtime(),'\t\n',time.localtime()[1])
15 # time.struct_time(tm_year=2021, tm_mon=11, tm_mday=8, tm_hour=10, tm_min=52, tm_sec=49, tm_wday=0, tm_yday=312, tm_isdst=0)
16 # 9个变量:
17 '''
18 tm_hour = """hours, range [0, 23]"""
19 tm_isdst = """1 if summer time is in effect, 0 if not, and -1 if unknown"""
20 tm_mday = """day of month, range [1, 31]"""
21 tm_min = """minutes, range [0, 59]"""
22 tm_mon = """month of year, range [1, 12]"""
23 tm_sec = """seconds, range [0, 61])"""
24 tm_wday = """day of week, range [0, 6], Monday is 0"""
25 tm_yday = """day of year, range [1, 366]"""
26 tm_year = """year, for example, 1993"""
27 '''
28 
29 
30 
31 '''
32 Convert a time tuple to a string according to a format specification.
33 See the library reference manual for formatting codes. When the time tuple
34 is not present, current time as returned by localtime() is used.
35 %Y  Year with century as a decimal number.
36 %m  Month as a decimal number [01,12].
37 %d  Day of the month as a decimal number [01,31].
38 %H  Hour (24-hour clock) as a decimal number [00,23].
39 %M  Minute as a decimal number [00,59].
40 %S  Second as a decimal number [00,61].
41 %z  Time zone offset from UTC.
42 %a  Locale's abbreviated weekday name.
43 %A  Locale's full weekday name.
44 %b  Locale's abbreviated month name.
45 %B  Locale's full month name.
46 %c  Locale's appropriate date and time representation.
47 %I  Hour (12-hour clock) as a decimal number [01,12].
48 %p  Locale's equivalent of either AM or PM.'''
49 
50 
51 # print(time.sleep(2))              #沉默一段时间
52 print(time.gmtime())                #不加就是把local时间转为UTC时间                 #返回元组tuple
53 print(time.gmtime(12345678900))     #从UNIX时间算起,过了1234567900秒之后的UTC时间    #返回元组tuple
54 print(time.localtime())             #系统时间                       #返回元组tuple
55 x = time.localtime()
56 print(type(x),x.tm_year,'\t\n',time.localtime()[1])
57 print(time.mktime(x))               #元组转换为时间戳
58 time_format = '%z %Y-%m-%d %H:%M:%S'
59 print(time_format.rjust(25,'=').ljust(30,'='))
60 print(time.strftime('%z %Y-%m-%d %H:%M:%S',x))             #格式化输出,从元组tuple转换为相应的格式,需要传入两个参数。
61 print(time.strptime('2021-11-08 12:01:54','%Y-%m-%d %H:%M:%S'))         #将格式化文本转为元组。
62 print(time.asctime(),time.asctime(x))               #将元组tuple转换为格式化字符串
63 print(time.ctime())                                 #将时间戳转换为格式化字符串
64 
65 
66 print(datetime.datetime.now())
67 print(datetime.datetime.now()+datetime.timedelta(4))
68 print(datetime.datetime.now()+datetime.timedelta(-4))
复制代码

 

 

复制代码
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # Author:woshinidaye
 4 import random
 5 '''
 6 print(random.random())  #0-1之间的浮点数
 7 print(random.uniform(0,10))     #在0-10之间取浮点数
 8 print(random.randint(1,300))    #1-300的整数,1、300都可以取到
 9 print(random.randrange(1,300,2))
10 a = []
11 n = 0
12 while n <= 300:
13     x = random.randrange(2,100,5)
14     a.append(x)
15     n = n+1
16 print(a,'\n',len(a))
17 
18 print(random.choice('fghj12345678'))
19 print(random.choice([1,2,2,3,4,4,12,31]))
20 
21 print(random.sample('213132',3))    #从字符串中随机取3位
22 print(random.sample([1,23,123,1,23,123,1,31,23,123,12,31,2334,4,24,234,3,423],3))
23 
24 random.shuffle(a)               #打乱顺序,洗牌
25 print(a)
26 '''
27 
28 
29 # 随机验证码
30 code = ''
31 for i in range(9):
32     a = random.randrange(0,9)
33     if a > 6 :
34         tmp = chr(random.randrange(65,90))      #大写子母转换为字符串
35     elif a < 3 :
36         tmp = chr(random.randrange(97,122))      #小写子母转换为字符串
37     else:
38         tmp = random.randint(0,9)
39     code = code + str(tmp)
40 print(code)
复制代码

 

posted @   woshinidaye  阅读(32)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· Qt个人项目总结 —— MySQL数据库查询与断言
点击右上角即可分享
微信分享提示