python 内置模块 优先级_Python之路——内置模块总结

time模块

在Python中,通常有这三种方式来表示时间:时间戳、元组(struct_time)、格式化的时间字符串:

(1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。

(2)格式化的时间字符串(Format String): ‘1999-12-06’

python中时间日期格式化符号:

%y 两位数的年份表示(00-99)

%Y 四位数的年份表示(000-9999)

%m 月份(01-12)

%d 月内中的一天(0-31)

%H 24小时制小时数(0-23)

%I 12小时制小时数(01-12)

%M 分钟数(00=59)

%S 秒(00-59)

%a 本地简化星期名称

%A 本地完整星期名称

%b 本地简化的月份名称

%B 本地完整的月份名称

%c 本地相应的日期表示和时间表示

%j 年内的一天(001-366)

%p 本地A.M.或P.M.的等价符

%U 一年中的星期数(00-53)星期天为星期的开始

%w 星期(0-6),星期天为星期的开始

%W 一年中的星期数(00-53)星期一为星期的开始

%x 本地相应的日期表示

%X 本地相应的时间表示

%Z 当前时区的名称

%% %号本身

(3)元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等

索引(Index)属性(Attribute)值(Values)

0

tm_year(年)

比如2011

1

tm_mon(月)

1 - 12

2

tm_mday(日)

1 - 31

3

tm_hour(时)

0 - 23

4

tm_min(分)

0 - 59

5

tm_sec(秒)

0 - 60

6

tm_wday(weekday)

0 - 6(0表示周一)

7

tm_yday(一年中的第几天)

1 - 366

8

tm_isdst(是否是夏令时)

默认为0

1 importtime2 #print(time.time())

3 #print(time.strftime("%Y-%m-%d %H:%M:%S"))

4 #print(time.strftime("%Y/%m/%d %H:%M:%S"))

5 #t1 = 1500000000

6 #t2 = 1600000000

7 #st1 = time.localtime(t1)

8 #st2 = time.localtime(t2)

9 #st3 = time.localtime(t2-t1)

10 #t4 = time.strftime('%Y-%m-%d %H:%M:%S',(st3[0]-1970,*st3[1:]))

11 #print(st3.tm_yday,st3.tm_hour,st3.tm_min,st3.tm_sec)

12

13 #def past_time(start_time = None):

14 #start_time = start_time if start_time else time.time()

15 #n = 0

16 #while n<100:

17 #time.sleep(0.5)

18 #end_time = time.time()

19 #st = time.gmtime(end_time-start_time)

20 #print('year:%s day:%s %s:%s:%s'% (st.tm_year-1970,st.tm_yday-1,st.tm_hour,st.tm_min,st.tm_sec))

21 #n += 1

22 #past_time()

23

24 #时间戳和结构化时间

25 #t = time.time()

26 #print(t)

27 #print(time.localtime(t))

28 #print(time.gmtime(t))

29 #print(time.mktime(time.localtime(t)))

30 #print(time.mktime(time.gmtime(8*3600)))

31

32 #asctime([tuple]) -> string

33 #print(time.asctime())

34 #Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.

35 #When the time tuple is not present, current time as returned by localtime()

36 #is used.

37

38 #strptime(string, format) -> struct_time

39 #Parse a string to a time tuple according to a format specification.

40 #print(time.strptime('2000-12.31','%Y-%m.%d'))

41 ## time.struct_time(tm_year=2000, tm_mon=12, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=366, tm_isdst=-1)

42

43 #print(time.strftime('%m/%d/%Y %H:%M:%S',time.localtime(3000000000))) # 01/24/2065 13:20:00

collections模块

在内置数据类型(dict、list、set、tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter、deque、defaultdict、namedtuple和OrderedDict等。

1.namedtuple: 生成可以使用名字来访问元素内容的tuple

2.deque: 双端队列,可以快速的从另外一侧追加和推出对象

3.Counter: 计数器,主要用来计数

4.OrderedDict: 有序字典

5.defaultdict: 带有默认值的字典

1 #from collections import namedtuple

2 #Point = namedtuple('point',['x','y','z'])

3 #p1 = Point(1,2,3)

4 #print(p1)

5 #print(p1.x,p1.y,p1.z)

6 #p2 = Point(2,3,4)

7 #print(p2.x,p2.y,p2.z)

8

9 #Card = namedtuple('card',['suits','number'])

10 #print(Card.__doc__)

11 #c1 = Card('红桃',3)

12 #print(c1.suits,c1.number)

13

14 #>>> from collections import namedtuple

15 #>>> Point = namedtuple('Point',['x','y'])

16 #>>> Point.__doc__

17 #'Point(x, y)'

18 #>>> p = Point(11,y=22)

19 #>>> p[0] + p[1]

20 #33

21 #>>> p.x+p.y

22 #33

23 #>>> a,b = p

24 #>>> a,b

25 #(11, 22)

26 #>>> a

27 #11

28 #>>> b

29 #22

30 #>>> d = p._asdict()

31 #>>> print(d)

32 #OrderedDict([('x', 11), ('y', 22)])

33 #>>> print(d['x'])

34 #11

35 #>>> Point(**d)

36 #Point(x=11, y=22)

37 #>>> p._replace(x=100)

38 #Point(x=100, y=22)

39

40 #队列

41 #import queue

42 #q = queue.Queue()

43 ## Create a queue object with a given maximum size.

44 ## If maxsize is <= 0, the queue size is infinite.

45 #q.put([1,2,3])

46 #q.put(5)

47 #q.put(6)

48 #print(q)

49 #print(q.qsize())

50 #print(q.get())

51 #print(q.get())

52 #print(q.get())

53 #print(q.qsize())

54

55 #from collections import deque

56 #dq = deque('adgas465a')

57 #print(dq)

58 ## print(dq.pop())

59 ## print(dq.pop())

60 #dq.append('a')

61 #dq.append('b')

62 #dq.appendleft('c')

63 #print(dq)

64 #print(dq.pop())

65 #print(dq.popleft())

66

67 #有序字典

68 #from collections import OrderedDict

69 #od = OrderedDict([('a',1),('b',2),('c',3)])

70 #print(od)

71 #print(od['a'])

72 #print(od['b'])

73 #for k in od:

74 #print(k,od[k])

75

76 #???????

77 #from collections import defaultdict

78 #d = defaultdict(lambda : 5)

79 #print(d)

80

81 #from collections import Counter

82 #Counter(): Create a new, empty Counter object. And if given, count elements

83 #from an input iterable. Or, initialize the count from another mapping

84 #of elements to their counts.

85 #c = Counter('sdfjkasjdflajs')

86 #print(c) # Counter({'s': 3, 'j': 3, 'a': 2, 'd': 2, 'f': 2, 'k': 1, 'l': 1})

87 #c['d'] -= 1

88 #print(c) # Counter({'j': 3, 's': 3, 'f': 2, 'a': 2, 'k': 1, 'd': 1, 'l': 1})

89

90 #c = Counter() # a new, empty counter

91 #print(c) # Counter()

92 #c = Counter('gallahad') # a new counter from an iterable

93 #print(c) # Counter({'a': 3, 'l': 2, 'd': 1, 'g': 1, 'h': 1})

94 #c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping

95 #print(c) # Counter({'a': 4, 'b': 2})

96 #c = Counter(a=4, b=2) # a new counter from keyword args

97 #print(c) # Counter({'a': 4, 'b': 2})

View Code

defaultdict

1 from collections importdefaultdict2

3 values = [11, 22, 33,44,55,66,77,88,99,90]4

5 my_dict =defaultdict(list)6

7 for value invalues:8 if value>66:9 my_dict['k1'].append(value)10 else:11 my_dict['k2'].append(value)

random模块

1 #import random

2 #random.random() # Get the next random number in the range [0.0, 1.0)

3 #print(random.random())

4

5 #random.uniform(a,b) # Get a random number in the range [a, b) or [a, b] depending on rounding.

6 #a = random.uniform(1,3)

7 #print(a)

8

9 #print(random.randint(1,5)) # 大于等于1小于等于5之间的整数

10

11 #print(random.choice([1,'23',[4,5]])) # 1或者23或者[4,5]

12 #print(random.choice(1)) # TypeError: object of type 'int' has no len()

13

14 #print(random.sample([1,'23',[4,5]],2)) # 随机返回[1,'23',[4,5]]中的两个元素,返回的是一个列表

15 #16 #print(random.sample(list({'k1':'v1','k2':'v2'}),2)) # ['k1', 'k2']

17

18 #打乱列表顺序

19 #item = [1,3,5,7,9]

20 #random.shuffle(item)

21 #print(item)

22

23 #生成验证码

24 #import random

25 #def v_code(n=5):

26 #code = ''

27 #for i in range(n):

28 #num = random.randint(0,9)

29 #alf = chr(random.randint(65,90))

30 #add = random.choice([num,alf])

31 #code += str(add)

32 #return code

33 #print(v_code())

34

35 #import random

36 #def v_code(n=5):

37 #code = ''

38 #for i in range(n):

39 #num = random.randint(0,9)

40 #alf = chr(random.randint(65,90))

41 #add = random.choice([num,alf])

42 #code = ''.join([code,str(add)])

43 #return code

44 #print(v_code())

View Code

os模块

1 importos2 #print(os.getcwd()) # Return a unicode string representing the current working directory

3

4 #os.chdir(r'E:\Python_Fullstack_S9') # Change the current working directory to the specified path.

5 #print(os.getcwd())

6

7 #os.makedirs('dirname1/dirname2/dirname3')

8 #os.removedirs('dirname1/dirname2/dirname3')

9 #os.mkdir('dirname1/dirname2/dirname3')

10

11 #print(os.listdir(r'E:\Python_Fullstack_S9'))

12

13 #stat 系统调用时用来返回相关文件的系统状态信息的

14 #sta=os.stat('collections_module.py')

15 #print(sta)

16 #os.stat_result(st_mode=33206, # 权限模式

17 #st_ino=5910974510945247, # inode number

18 #st_dev=284297630, # device

19 #st_nlink=1, # number of hard links

20 #st_uid=0, # 所有用户的user id

21 #st_gid=0, # 所有用户的group id

22 #st_size=1641, # 文件的大小,以位为单位

23 #st_atime=1515569689, # 文件最后访问时间

24 #st_mtime=1515569689, # 文件最后修改时间

25 #st_ctime=1515566554) # 文件创建时间

26

27 #python os.stat() 和 stat模块详解: http://www.cnblogs.com/maseng/p/3386140.html

28

29 #print(os.sep) # \

30

31 #os.system('dir')

32 #ret = os.popen('dir').read()

33 #print(ret)

34 #print(os.environ)

35

36 #print(os.getcwd()) # E:\Python_Fullstack_S9\Python_Fulldeck_S9_20171214\day19

37 #print(os.path.split(os.getcwd())) # ('E:\\Python_Fullstack_S9\\Python_Fulldeck_S9_20171214', 'day19')

38

39 #print(os.path.join(r'C:\Users\Administrator','user','local'))

40 #print(os.getcwd())

41 #print(os.path.getsize(os.path.join(os.getcwd(),'time_module.py')))

View Code

sys模块

sys模块是与python解释器交互的一个接口

sys.argv 命令行参数List,第一个元素是程序本身路径

sys.exit(n) 退出程序,正常退出时exit(0),错误退出sys.exit(1)

sys.version 获取Python解释程序的版本信息

sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值

sys.platform 返回操作系统平台名称

1 importsys2 #print(sys.platform)

3 #print(sys.version)

4

5 #print(sys.path.clear())

6

7 #ret = sys.argv

8 #name = ret[1]

9 #pwd = ret[2]

10 #if name == 'alex' and pwd == 'alex3714':

11 #print('success')

12 #else:

13 #print('error')

14 #sys.exit()

15 #print('go on ....')

序列化模块

1 #import json

2 #json: dumps and loads

3 #dic = {"k":'v'}

4 #dic = (1,2,3,4)

5 #dic = {1:'a',2:'b',3:'c'}

6 #s = json.dumps(dic)

7 #print(dic,type(dic))

8 #print(s,type(s))

9 #dic2 = json.loads(s)

10 #print(dic2,type(dic2))

11

12 #json: dump and load

13 #dic = {1:'a刘',2:'b严魁'}

14 #f = open('fff','w',encoding='utf-8')

15 #json.dump(dic,f,ensure_ascii=False)

16 ##If ``ensure_ascii`` is false, then the strings written to ``fp`` can

17 ## contain non-ASCII characters if they appear in strings contained in

18 ## ``obj``. Otherwise, all such characters are escaped in JSON strings.

19 #f.close()

20

21 #f2 = open('fff','r',encoding='utf-8')

22 #dic2 = json.load(f2)

23 #f2.close()

24 #print(dic2,type(dic2))

25

26 #l = [{'k1':1111},{'k2':2222},{'k3':3333}]

27 #f = open('file','w')

28 #import json

29 #for dic in l:

30 #str_dic = json.dumps(dic)+'\n'

31 #f.write(str_dic)

32 #f.close()

33

34 #import json

35 #l = []

36 #f = open('file')

37 #for line in f:

38 #d = json.loads(line.strip())

39 #l.append(d)

40 #f.close()

41 #print(l)

View Code

re模块

1 importre2 s = 'eva egon yuan'

3

4 #ret = re.findall('a',s) # 返回所有满足匹配条件的结果,放在列表里

5 #print(ret) # ['a', 'a']

6

7 #ret = re.search('a',s)

8 #print(type(ret)) #

9 #print(ret.group())

10 #函数会在字符串内查找模式匹配,直到找到第一个匹配,然后返回一个包含匹配信息的对象,

11 #该对象通过group()方法得到匹配的字符串,如果字符串没有匹配,则返回None

12

13 #ret = re.match('a','abcabcabc').group() # 同search,不过只在字符串开始处进行匹配

14 #print(ret) # a

15 #ret = re.match('','abcabcabc').group()

16 #print(ret)

17

18 #ret = re.split('[ab]','abcd') # 先按a进行分割,再按b进行分割

19 #print(ret) # ['', '', 'cd']

20

21 #ret = re.sub('\d','H','eva3egon4yuan4',1) # 将数字替换成H,参数1表示只替换一个

22 #print(ret) # evaHegon4yuan4

23 #ret = re.sub('\d','H','eva3egon4yuan4')#默认替换全部

24 #print(ret)

25

26 #ret = re.subn('\d','H','eva3egon4yuan4') # 将数字替换成H,返回元组:(替换的结果,替换了多少次)

27 #print(ret) # ('evaHegonHyuanH', 3)

28

29 #obj = re.compile('\d{3}') # 将正则表达式编译成一个正则表达式对象,规则要匹配的是3个数字

30 #ret = obj.search('abc123eeee') # 正则表达式对象调用search,参数为待匹配的字符串

31 #print(ret.group()) # 123

32

33 #obj = re.compile('\d{3}')

34 #ret = obj.findall('abc123ee123445df3452345245ee')

35 #print(ret)

36

37 #ret = re.finditer('\d+','ds3sy4784a64gs65dfg4s65dg') # 返回一个存放匹配结果的迭代器

38 #print(ret) #

39 #print(next(ret).group())

40 #print(next(ret).group())

41 #print([i.group() for i in ret])

42

43

44 #findall 的优先级查询:

45 ## ret = re.findall('www.(baidu|oldboy).com','www.oldboy.com')

46 ## print(ret) # ['oldboy']` 这是因为findall会优先把匹配结果组里内容返回,如果想要匹配结果,取消权限即可

47 #48 #ret = re.findall('www.(?:baidu|oldboy).com','www.oldboy.com')

49 #print(ret) # ['www.oldboy.com']

50

51 #split的优先级查询

52 #ret = re.split('\d+','eva3egon4yuan')

53 #print(ret) # ['eva', 'egon', 'yuan'] 没有(),则没有保留所匹配的项

54 #ret = re.split('(\d+)','eva3egon4yuan')

55 #print(ret) # ['eva', '3', 'egon', '4', 'yuan'] 有(),能够保留所匹配的项

56

57 #匹配标签

58 #ret = re.search("\w+)>\w+(?P=tag_name)>",'

hello

')

59 #可以在分组中利用?P的形式给分组起名字

60 #获取的匹配结果可以直接用group('名字')拿到对于的值

61 #print(ret.group()) #

hello

62 #print(ret.group('tag_name')) # h1

63

64 #ret = re.search(r"\w+\1>","

hello

")

65 #如果不给组起名字,也可以用\序号来找到对应的组,表示要找到的内容和前面的组内容一致

66 #获取的匹配结果可以直接用group(序号)拿到对应的值

67 #print(ret.group(1)) # h1

68 #print(ret.group()) #

hello

69 #print(type(ret))

70

71 #匹配整数

72 #ret = re.findall(r"\d+","1-2*(60+(-40.35/5)-(-4*3))")

73 #print(ret) # ['1', '2', '60', '40', '35', '5', '4', '3']

74 #ret = re.findall(r"-?\d+\.\d*|(-?\d+)","1-2*(60+(-40.35/5)-(-4*3))")

75 #print(ret) # ['1', '-2', '60', '', '5', '-4', '3']

76 ret = re.findall(r"-?\d+","1-2*(60+(-40.35/5)-(-4*3))")77 print(ret) #['1', '-2', '60', '', '5', '-4', '3']

78 #ret.remove('')

79 #print(ret) # ['1', '-2', '60', '5', '-4', '3']

80

81 #数字匹配

82 #1、 匹配一段文本中的每行的邮箱

83 #http://blog.csdn.net/make164492212/article/details/51656638

84 #85 #2、 匹配一段文本中的每行的时间字符串,比如:‘1990-07-12’;

86 #87 #分别取出1年的12个月(^(0?[1-9]|1[0-2])$)、

88 #一个月的31天:^((0?[1-9])|((1|2)[0-9])|30|31)$

89 #90 #3、 匹配qq号。(腾讯QQ号从10000开始) [1,9][0,9]{4,}

91 #92 #4、 匹配一个浮点数。 ^(-?\d+)(\.\d+)?$ 或者 -?\d+\.?\d*

93 #ret = re.findall('-?\d+\.?\d*',"-1.35")

94 #print(ret)

95 #96 #5、 匹配汉字。 ^[\u4e00-\u9fa5]{0,}$

97 #98 #6、 匹配出所有整数

View Code

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值