Python之string模块的使用

string模块作用

string模块在最早的Python版本中就已经有了。以前这个模块中提供的很多函数已移值为str对象的方法,不过这个模块仍保留了很多有用的常量和类的处理str对象。

1、把字符串所有单词首字母变成大写

import string

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

print(s)
print(string.capwords(s))
string_capwords.py

运行效果

The quick brown fox jumped over the lazy dog.
The Quick Brown Fox Jumped Over The Lazy Dog.

2、字符串模板格式化

import string

values = {'var': 'foo'}

t = string.Template("""
Variable        : $var 
Escape          : $$  
Variable in text: ${var}iable 
""")
# ${var} #获取字典的值
# $var  #获取字典的键
print('TEMPLATE:', t.substitute(values))

s = """
Variable        : %(var)s
Escape          : %%
Variable in text: %(var)siable
"""

print('INTERPOLATION:', s % values)

s = """
Variable        : {var}
Escape          : {{}}
Variable in text: {var}iable
"""

print('FORMAT:', s.format(**values))
string_template.py

运行效果

TEMPLATE: 
Variable        : foo 
Escape          : $  
Variable in text: fooiable 

INTERPOLATION: 
Variable        : foo
Escape          : %
Variable in text: fooiable

FORMAT: 
Variable        : foo
Escape          : {}
Variable in text: fooiable

3、模板变量值没有设置时,异常的处理

import string

values = {'var': 'foo'}

t = string.Template("$var is here but $missing is not provided")

try:
    print('substitute()     :', t.substitute(values))
except KeyError as err:
    print('ERROR:', str(err))

print('safe_substitute():', t.safe_substitute(values))
string_template_missing.py

运行效果

ERROR: 'missing'
safe_substitute(): foo is here but $missing is not provided

4、高级模板,指定限定符和匹配正则表达式,进行字符串模板替换

import string


class MyTemplate(string.Template):
    delimiter = '%'
    idpattern = '[a-z]+_[a-z]+'


template_text = '''
  Delimiter : %%
  Replaced  : %with_underscore
  Ignored   : %notunderscored
'''

d = {
    'with_underscore': 'replaced',
    'notunderscored': 'not replaced',
}

t = MyTemplate(template_text)
print('Modified ID pattern:')
print(t.safe_substitute(d))
string_template_advanced.py

 运行效果

Modified ID pattern:

  Delimiter : %
  Replaced  : replaced
  Ignored   : %notunderscored

5、打印出4个命名组,分别捕获转义定界符、命名变量、加括号的变量名和不合法的定界符模式。

import string

t = string.Template('$var')
print(t.pattern.pattern)
string_template_defaultpattern.py

运行效果

    \$(?:
      (?P<escaped>\$) |   # Escape sequence of two delimiters
      (?P<named>(?a:[_a-z][_a-z0-9]*))      |   # delimiter and a Python identifier
      {(?P<braced>(?a:[_a-z][_a-z0-9]*))}  |   # delimiter and a braced identifier
      (?P<invalid>)              # Other ill-formed delimiter exprs
    )

6、自定义正式表达式,匹配字符和替换字符的示例

import string


class MyTemplate(string.Template):
    delimiter = '{{'
    pattern = r'''
    \{\{(?:
    (?P<escaped>\{\{)|
    (?P<named>[_a-z][_a-z0-9]*)\}\}|
    (?P<braced>[_a-z][_a-z0-9]*)\}\}|
    (?P<invalid>)
    )
    '''


t = MyTemplate('''
{{{{
{{var}}
''')

print('MATCHES:', t.pattern.findall(t.template))
print('SUBSTITUTED:', t.safe_substitute(var='replacement'))
string_template_newsyntax.py

运行效果

MATCHES: [('{{', '', '', ''), ('', 'var', '', '')]
SUBSTITUTED: 
{{
replacement

7、string模块包含大量的常量值,如果开发过程中有需要的时候,可以获取出来

import inspect
import string


def is_str(value):
    return isinstance(value, str)


for name, value in inspect.getmembers(string, is_str):
    if name.startswith('_'):
        continue
    print('%s=%r\n' % (name, value))
string_constants.py

 

运行效果

ascii_letters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

ascii_lowercase='abcdefghijklmnopqrstuvwxyz'

ascii_uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

digits='0123456789'

hexdigits='0123456789abcdefABCDEF'

octdigits='01234567'

printable='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

punctuation='!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

whitespace=' \t\n\r\x0b\x0c'

 

posted @ 2020-07-02 14:17  小粉优化大师  阅读(247)  评论(0编辑  收藏  举报