Python 3标准库课件第一章
发布时间:2020-12-20 10:17:04 所属栏目:Python 来源:网络整理
导读:第一章 文本处理工具:str类 string.Template 简便的方法来构建比str对象特性更丰富的字符串。 Web框架定义的模板或Python Package Index提供的扩展模块相比,尽管string.Template没有那么丰富的特性,但它确实能很好地支持用户可修改的模板,这些模板需要将
第一章
文本处理工具:str类
string.Template 简便的方法来构建比str对象特性更丰富的字符串。
Web框架定义的模板或Python Package Index提供的扩展模块相比,尽管string.Template没有那么丰富的特性,但它确实能很好地支持用户可修改的模板,这些模板需要将动态值插入到静态文本中。
textwrap模块包含格式化段落文本的工具,可以限制输出的宽度、增加缩进,以及插入换行符从而能一致地自动换行。
1.1 string:文本常量和模板
string模块在最早的Python版本中就已经有了。以前这个模块中提供的很多函数已经移植为str对象的方法,不过这个模块仍保留了很多有用的常量和类来处理str对象。这里将重点讨论这些常量和类。
? 1.1.1?函数 ??? 函数capwords()会把一个字符串中的所有单词首字母大写。 ???????代码清单 1-1:string_capwords.py ???????-------------------------------- ???????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. ------------------------------------------ 这个代码的结果等同于先调用split(),把结果列表中的单词首字母大写,然后调用join()来合并结果。
1.1.2?模板
字符串模板是PEP 292新增的部分,将作为内置拼接语法的代替做法。使用string.Template拼接时,要在名字前面加前缀$来标识变量(例如,$var)。或者,如果有必要区分变量和周围的文本,可以用大括号
包围变量(例如,${var})。 下面这个例子对一个简单模板,使用%操作符的类似字符串拼接以及使用str.format()的新格式化字符串语法做了比较: string.Template
???????代码清单1-2:string_template.py
???????------------------------------ ???????import string
???????values = {‘var‘: ‘foo‘}
??????? ???????t = string.Template(""" ???????Variable? :$var ???????Escape??? :$$ ???????Variable in text :${var}iable ???????""" ???????) ??????? ???????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}siable ???????""" ??????? ???????print(‘FORMAT:‘,s.format(**values)) ???????----------------------------------- 在前面两种情况中,触发字符($或%)要重复两次来进行转义。在格式化语法中,需要重复{和}来转义。 ----------------------------------------------- TEMPLATE: Variable? :foo Escape??? :$ Variable in text :fooiable
INTERPOLATION
Variable? :foo Escape? :% Variable in text :fooiable???????
FORMAT:
Variable? : %foo Escape??? : {} Variable in text :foosiable ------------------------------------------------- 模板与字符串拼接或格式化的一个关键区别是,它不考虑参数的类型。值会转换为字符串,再将字符串插入结果。这里没有提供格式化选项。例如,没有办法控制使用几位有效数字来表示一个浮点值。 不过,这也有一个好处,通过使用sfe_substitute()方法,可以避免未能向模板提供所需的所有参数值时可能产生的异常。 ------------------------------------------------- ???????代码清单1-3:string_template_missing.py ???????--------------------------------------- ???????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)) ???????--------------------------------------- ???????结果: ???????ERROR: ‘missing‘ ???????safe_substitute(): foo is here but $missing is not provided ???????--------------------------------------- 由于values字典中没有missing的值,所以substitute()会产生一个KeyError。safe_substitute()则不同,它不会抛出这个错误,而是会捕获这个错误并保留文本中的变量表达式。
1.1.3 高级模板
可以调整string.Template在模板体中查找变量名所使用的正则表达式模式,以改变它的默认语法。为此,一种简单的方法是修改delimiter和idpattern类属性。 ???????代码清单1-4:string_template_advanced.py ???????----------------------------------------- ???????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)) ???????------------------------------------------ ???????Modified ID pattern:
???????Delimiter : % ???????Replaced? : replaced ???????Ignored?? : %notunderscored ???????------------------------------------------ ???????
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |