当前位置: 首页 > 编程笔记 >

Python的string模块中的Template类字符串模板用法

越心水
2023-03-14
本文向大家介绍Python的string模块中的Template类字符串模板用法,包括了Python的string模块中的Template类字符串模板用法的使用技巧和注意事项,需要的朋友参考一下

string.Template()
string.Template()内添加替换的字符, 使用"$"符号, 或 在字符串内, 使用"${}"; 调用时使用string.substitute(dict)函数.
可以通过继承"string.Template", 覆盖变量delimiter(定界符)和idpattern(替换格式), 定制不同形式的模板.

代码:

# -*- coding: utf-8 -*- 

import string 
 
template_text = ''''' 
  Delimiter : %% 
  Replaced : %with_underscore 
  Ingored : %notunderscored 
''' 
 
d = {'with_underscore' : 'replaced', 
   'notunderscored' : 'not replaced'} 
 
class MyTemplate(string.Template): 
  delimiter = '%' 
  idpattern = '[a-z]+_[a-z]+' 
   
t = MyTemplate(template_text) 
print('Modified ID pattern: ') 
print(t.safe_substitute(d)) 

输出:

Modified ID pattern:  
 
  Delimiter : % 
  Replaced : replaced 
  Ingored : %notunderscored 

注意: 定界符(delimiter)为"%", 替换模式(idpattern)必须包含下划线, 所以第2个没有进行替换.

正则替换

string.Template的pattern是一个正则表达式, 可以通过覆盖pattern属性, 定义新的正则表达式.
如: 使用新的定界符"{{", 把{{var}}作为变量语法.

代码:

import string 
 
t = string.Template('$var') 
print(t.pattern.pattern) 
 
class MyTemplate(string.Template): 
  delimiter = '{{' 
  pattern = r''''' 
  \{\{(?: 
   (?P<escaped>\{\{) |  # Escape sequence of two delimiters 
   (?P<named>[_a-z][_a-z0-9]*)\}\}   |  # delimiter and a Python identifier 
   {(?P<braced>[_a-z][_a-z0-9]*)}\}\}  |  # delimiter and a braced identifier 
   (?P<invalid>)       # Other ill-formed delimiter exprs 
  ) 
  ''' 
   
t2 = MyTemplate(''''' 
{{{{ 
{{var}} 
''') 
 
print('MATCHES: ', t2.pattern.findall(t2.template)) 
print('SUBSTITUTED: ', t2.safe_substitute(var='replacement')) 

输出:

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

字符串模板的安全替换(safe_substitute)
字符串模板(sting.Template), 替换时, 使用substitute(), 未能提供模板所需的全部参数值时, 会发生异常.
如果使用safe_substitute(), 即安全替换, 则会替换存在的字典值, 保留未存在的替换符号.

代码:

import string 
 
values = {'var' : 'foo'} 
 
t = string.Template('''''$var is here but $ missing is not provided! ''') 
 
 
try: 
  print 'substitute() : ', t.substitute(values) 
except ValueError as err: 
  print 'Error:', str(err) 
   
print 'safe_substitude() : ', t.safe_substitute(values) 

输出:

substitute() : Error: Invalid placeholder in string: line 1, col 18 
safe_substitude() : foo is here but $ missing is not provided!  


 类似资料:
  • 问题内容: 在Python程序中,给定带有函数名称的字符串的最佳方法是什么。例如,假设我有一个模块,我有一个内容为的字符串。最好的通话方式是什么? 我需要获取函数的返回值,这就是为什么我不只是使用。我想出了如何通过使用eval定义一个返回该函数调用结果的函数来执行此操作的方法,但是我希望有一种更优雅的方法来执行此操作。 问题答案: 假设模块与方法: 你可以将第2行和第3行缩短为: 如果这对你的用例

  • 本文向大家介绍Vue中的字符串模板的使用,包括了Vue中的字符串模板的使用的使用技巧和注意事项,需要的朋友参考一下 1、HTML模板和字符串模板 HTML模板:直接在HTML页面挂载的模板。(即非字符串模板) 非字符串模板:在单文件里用 <template></template> 指定的模板,换句话说,写在 html 中的就是非字符串模板。 字符串模板:在js字符串中定义的模板。 2、Props属

  • 问题内容: 我有以下模板: 我在执行模板时传递了一个字符串。 但是,出现以下错误: 如何比较模板中的字符串? 问题答案: 是函数,而不是运算符。它以以下形式调用:(不是)。 您可以通过将操作数从的侧面移动到之后来修复模板:

  • ES6引入了一种通过反引号( ` )标记的新的字符串文字类型。 这些字符串文字可以包括换行符,并且有一个新的机制用于将变量插入字符串:

  • 下面就是Array#pack、String#unpack中所用到的模板字符的一览表。模板字符后面可以跟上表示"长度"的数字。若使用'*'来取代"长度"的话, 则表示"剩下的所有字符"之意。 长度的定义因模板字符的不同而有所差异, 大体上像 "iiii" 这样的连续字符可以写成 "i4" 这个样子。 在下面的说明中, short和long分别表示长度为2和4字节的数值(也就是通常32位机器所指的

  • Kotlin有一个很好的特性,叫做字符串模板。我真的很喜欢。 但是否可以在模板中设置任何格式?例如,我想在kotlin中设置字符串模板中的Double格式,至少要在小数分隔符后设置一些位数: