擅长:python、mysql、java
<p>您可以匹配正则表达式,并使用替换函数调整单位(并更改单位名称)</p>
<pre><code>import re
r = re.compile("(\d+) cubits")
s = "So make yourself an ark of cypress wood; make rooms in it and coat it with pitch inside and out. This is how you are to build it: The ark is to be 300 cubits long, 50 cubits wide and 30 cubits high. (Genesis 6:14-15)"
print(r.sub(lambda m : "{} feet".format(int(float(m.group(1))*1.5)),s))
</code></pre>
<p>结果:</p>
<blockquote>
<p>So make yourself an ark of cypress wood; make rooms in it and coat it with pitch inside and out. This is how you are to build it: The ark is to be 450 feet long, 75 feet wide and 45 feet high. (Genesis 6:14-15)</p>
</blockquote>
<p>当正则表达式匹配后跟“cubits”的数字时,它调用lambda回调,解析该数字,乘以1.5,转换回整数,并用新的单位重新格式化输出</p>
<p>这将不得不微调为1英尺,并可以使用转换字典,如果有更多的单位转换</p>