ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python:String-模版替换操作

2021-05-02 13:59:31  阅读:201  来源: 互联网

标签:String missing python 模版 var 66 variable substitute string


capwords函数

将序列中的每个单词变成首字母大写


def capwords(s, sep=None):
    """capwords(s [,sep]) -> string

    Split the argument into words using split, capitalize each
    word using capitalize, and join the capitalized words using
    join.  If the optional second argument sep is absent or None,
    runs of whitespace characters are replaced by a single space
    and leading and trailing whitespace are removed, otherwise
    sep is used to split and join the words.

    """
    return (sep or ' ').join(x.capitalize() for x in s.split(sep))

从源码中我们在return处可以看到是先使用了split进行切割(默认走空格切割),然后在使用join进行结果的合并

栗子:


import string


s = 'this is old old man'
print(s)

print(string.capwords(s))

运行后的结果如下


this is old old man
This Is Old Old Man

Process finished with exit code 0

从运行结果看,可以清晰的看出原本小写的单词,最后变成首字母大写了...

 

模版

模版拼接也是内置拼接的代替方法,使用string.Template拼接时,要在名字前加前缀$来标识变量(例如:$var),如果要和周围的变量区分的话,那就加一个花括号(例如:${var})。

栗子:


import string


values = {'var': 66}


t = string.Template("""
variable :   $var
escape  :   $var
variable in text    :   ${var}和我拼接
""")
print(t.substitute(values))
print(t.safe_substitute(values))

运行后的结果如下

variable :   66
escape  :   66
variable in text    :   66和我拼接


variable :   66
escape  :   66
variable in text    :   66和我拼接

从上述结果来看,数据是已经替换成功了,唯一不好的就是数据都是字符型,如果参数的类型需要更换,就得再做一层处理... 具体怎么做可自行动手操作

 

substitute和safe_substitute方法

  1. substitute处理替换时,存在就替换,不存在则抛KeyError错误

  2. safe_substitute处理替换时,存在就替换,不存在就是原参数返回,不会抛错误

栗子:substitute方法


import string

values = {'var': 66}


t = string.Template("""
variable :  $var
escape  :   $var
missing :   $missing
variable in text    :   ${var}和我拼接
""")
print(t.substitute(values))

运行后的结果如下

Traceback (most recent call last):
  File "/Users/lifeng/python-projects/Test/pythonScripts/python_string.py", line 21, in <module>
    print(t.substitute(values))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/string.py", line 121, in substitute
    return self.pattern.sub(convert, self.template)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/string.py", line 114, in convert
    return str(mapping[named])
KeyError: 'missing'

从上述运行结果可以看出,报错是KeyError:'missing'

 

栗子:safe_substitute方法


import string

values = {'var': 66}


t = string.Template("""
variable :  $var
escape  :   $var
missing :   $missing
variable in text    :   ${var}和我拼接
""")
print(t.safe_substitute(values))

运行后的结果如下


variable :  66
escape  :   66
missing :   $missing
variable in text    :   66和我拼接


Process finished with exit code 0

从上述运行结果看,没有参数替换的参数,就原参数返回,且不会报错

 

 

可能有些时候我们觉得$符号不舒服,想更换一种符号用,那这样我们就可以继承Template进行重写操作


class MyTemplate(string.Template):
    delimiter = '!'


l = MyTemplate("""
variable :  !var
escape  :   !var
missing :   $missing
variable in text    :   ${var}和我拼接
""")
print(l.substitute(values))

在上述代码中我们只是把源码中的delimiter变量的值修改了,运行后的结果如下

上述代码中我们把定界符$符号修改成了!符号,参数替换也是成功的,当然你如果想重写别处代码也是可以的,因为Template这个类本身就有正则表达式,只要源码看得懂那就改起来吧...

 

 

以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,评论区留言会及时修正发布,谢谢!

 


 

未完,待续…

一直都在努力,希望您也是

 

微信搜索公众号:就用python

 

 

标签:String,missing,python,模版,var,66,variable,substitute,string
来源: https://blog.csdn.net/LIFENG0402/article/details/116353203

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有