我正在学习python并使用字符串来寻找使用字典
进行字符串替换的更好方法我有一个字符串,其中包含我的自定义占位符,如下所示:
placeholder_prefix = '$['
placeholder_suffix = ']'
dict={'key1':'string','key2':placeholders}
msg='This $[key1] contains custom $[key2]'
我希望占位符('前缀 - 后缀'和'键')应替换为' value'来自下面的字典:
' 此字符串包含自定义占位符'
我能够收到以下消息:'此[字符串]包含自定义[占位符]'通过写功能:
def replace_all(text):
for key, value in brand_dictionary.iteritems():
text = text.replace(key, value).replace('$[', '[')
return text
我可以尝试不同的替换来删除' $ ['和']'但这可以取代作为信息本身的一部分而包含的任何字符(例如' $',' [',']')(不作为占位符)。所以我想避免这种情况,只替换自定义占位符。
我可以想到正则表达式(对于占位符),但由于我的消息包含多个键,所以看起来它可能没用?
在python中有更好的方法吗?
答案 0 :(得分:3)
试试这个:
dict={key1:'string',key2:placeholders}
msg='This {key1} contains custom {key2}'.format(**dict)
示例我跑了:
>>> msg="hello {a} {b}"
>>> t={"a":"aa","b":"bb"}
>>> msg="hello {a} {b}".format(**t)
>>> msg
'hello aa bb'
答案 1 :(得分:0)
如果您可以更改占位符,则可以使用 - %(key)s
- 和%
运算符自动在这些位置应用dict。
Exmaple -
>>> dict={'key1':'string','key2':'placeholders'}
>>> msg='This %(key1)s contains custom %(key2)s'
>>> print(msg%dict)
This string contains custom placeholders
答案 2 :(得分:0)
作为一种更通用的方法,您可以将>>> d={'key1':'string','key2':'placeholders'}
>>> re.sub(r'\$\[([^\]]*)\]',lambda x:d.get(x.group(1)),msg)
'This string contains custom placeholders'
与正确的替换函数一起使用:
In [123]: d={'key1':'string','key2':'placeholders'}
...: msg='This {key1} contains custom {key2}'
...:
...:
In [124]: msg.format(**d)
Out[124]: 'This string contains custom placeholders'
使用正则表达式的优点是它拒绝匹配不具有预期格式的字符串中的占位符字符!
或者更简单的方法是使用字符串格式如下:
f-strings
或者如果变量的数量不是那么大,你可以将密钥作为当前命名空间中可访问的变量,然后使用自Python-3.6以来引入的功能In [125]: key1='string'
...: key2= 'placeholders'
...: msg=f'This {key1} contains custom {key2}'
...:
In [126]: msg
Out[126]: 'This string contains custom placeholders'
,而不是使用字典:
if user.respond_to?(:has_spree_role?) and user.has_spree_role?('supplier')
can :manage, Spree::Offer
end
答案 3 :(得分:0)
请考虑使用像Mako(http://www.makotemplates.org/)这样的现有模板库,而不是自己动手。
他们已经做了你想做的一切,加上你还没想过的很多事情。
(是的,它们也适用于生成非HTML文本)
答案 4 :(得分:0)
另一个选项包括使用字符串替换进行格式化:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="width:100%; height:auto; display:flex; flex-direction:column; padding:0px 0px; ">
<span style="width:100%; height:150px; display:flex; justify-content:center; align-items:center; font-family:Ostrich Sans Bold; font-size:65px; font-weight:900; color:rgba(0,0,0,0.9);">NUESTROS BOCATAS</span>
<div style="width:100%; height:auto; display:flex; justify-content:space-around; flex-wrap:wrap;">
<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
<div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
<div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">
<i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
</div>
</div>
<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
<a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
</div>
</div>
<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
<div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
<div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">
<i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
</div>
</div>
<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
<a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
</div>
</div>
<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
<div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
<div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">
<i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
</div>
</div>
<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
<a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
</div>
</div>
<div style="width:46%; height:500px; display:flex; flex-direction:column; margin:25px 0px;">
<div style="width:100%; height:70%; position:relative; box-shadow:4px 4px 4px rgba(0,0,0,0.2); background-size:cover; background-position:center; background-image:url('https://tallypress.com/wp-content/uploads/2017/08/top-10-western-food-catering-services-in-kl-and-selangor.jpg');">
<div class="toggle_hide hide" style="display:flex; width:100%; height:100%; position:absolute; top:0px; left:0px; z-index:999; background-color:rgba(0,0,0,0.6);">
<i class="fa fa-info" style="margin:auto; font-size:25px; color:rgba(255,255,255,0,7);"></i>
</div>
</div>
<div style="width:100%; height:30%; padding:10px; display:flex; flex-direction:column; ">
<a href="" style="text-align:center; font-family:Ostrich Sans Heavy Bold; font-size:22px; color:black; font-weight:600; margin-bottom:15px;">Titulo</a>
<a href="" style="font-family:Aleo; font-size:19px; color:rgba(0,0,0,0.8); font-weight:600;">Grade A Nebraska chuck beef, on a sponge bun with lettuce, onion, tomato, and thousand island.</a>
</div>
</div>
</div>
</div>