开发者

How to escape character '.' in python string replacement via the operator %

开发者 https://www.devze.com 2023-02-13 07:00 出处:网络
I want to do the following \"@file %(unitname).C\" % {\'unitname开发者_如何学运维\':\'Test\'} but it complains about the \'.C\'. How can I escape the \'.\' character?You don\'t. You fix the format

I want to do the following

"@file %(unitname).C" % {'unitname开发者_如何学运维':'Test'}

but it complains about the '.C'. How can I escape the '.' character?


You don't. You fix the format specifier.

"@file %(unitname)s.C" % {'unitname':'Test'}


Another option is,

"@file {unitname}.C".format(unitname='Test')

This may be preferable, since format() will be the new standard in Python 3.

0

精彩评论

暂无评论...
验证码 换一张
取 消