2017-01-18 72 views
0

我想申请十进制格式的十进制字符串中的句子 (The temperature of $place is $value degree $unit)将其转化为(The temperature of room is 45 degree Celsius)申请十进制格式为十进制的字符串+数字的句子

我使用python的模板库safe_substitute方法改变我的字符串para-metered内容。 我需要使用特定格式({0:.2f})将相同句子中的一个十进制值(来自上面句子的$value)格式化。如何做到这一点的任何想法,请回应。

+1

你为什么不使用'str.format'? – falsetru

回答

0

您可以使用format来控制你要多少小数在safe_substitute显示如下:

from string import Template 
s = Template('(The temperature of $place is $value degree $unit)') 
print(s.safe_substitute(place='room', value='{:.2f}'.format(45), unit='Celsius')) 
+0

这个技巧。感谢tomcy的回答。 – J2G

相关问题