Fork me on GitHub

【进阶07】【自学笔记】Python标准库实例补充

一、capwords() 将字符串中所有单词的首字母大写。

import string

s = 'The quick brown fox jumped over the lazy dog.'

print(s)
print(string.capwords(s))
The quick brown fox jumped over the lazy dog.
The Quick Brown Fox Jumped Over The Lazy Dog.

  

二、re正则表达式

#re 最常见的用法就是在文本中查找模式。 search() 函数接受目标模式和要扫描的文本
import re
pattern='this'
text='Does this text match the pattern?'
match =re.search(pattern,text)
s=match.start()
e=match.end()

print(text[s:e])
print(s,e)
#多重匹配,匹配对应字符串索引下标值
import re
text = 'abbaaabbbbaaaaa'
pattern = 'ab'

for match in re.finditer(pattern,text):
    s=match.start()
    e=match.end()
    print('Found {!r} at {:d}:{:d}'.format(
        text[s:e], s, e))

 

三、enum – 枚举类型

 我们通过使用 class 语法创建一个继承自 Enum 的类并且加入描述值的类变量来定义一个新的枚举类型。

  

import enum

class BugStatus(enum.Enum):

    new = 7
    incomplete = 6
    invalid = 5
    wont_fix = 4
    in_progress = 3
    fix_committed = 2
    fix_released = 1

print('\nMember name: {}'.format(BugStatus.wont_fix.name))
print('Member value: {}'.format(BugStatus.wont_fix.value))

 迭代

对 enum 类 的迭代将产生独立的枚举成员。

import enum
class BugStatus(enum.Enum):
    new = 7
    incomplete = 6
    invalid = 5
    wont_fix = 4
    in_progress = 3
    fix_committed = 2
    fix_released = 1

for status in BugStatus:
    print('{:15} = {}'.format(status.name, status.value)) 

python魔法指南转载:

https://magic.iswbm.com/chapters/p05.html

 

  

posted @ 2022-01-18 23:56  橘子偏爱橙子  阅读(31)  评论(0编辑  收藏  举报