我有一个问题,我可以将½转换成0.5,但我不知道如何将1.5转换成1.5或2.5转换成2.5。下面是链接. 有一行1.5公斤,我想如果你能告诉我一个更高级的replace()参数或其他东西,会对我有帮助。这是我已经有的转换为0.5的方法。
raw_string = re.sub(r'\s{2,}', ' ', ingredient.get_text(strip=True))
raw_string = raw_string.replace(u'\u00BD', "0.5")
一个可能的解决方案。
# this converts any integer followed by ½
raw_string = re.sub(r'(\d+)\s*'+u'\u00BD', r'\1.5', raw_string)
# this takes care of lone ½
raw_string = raw_string.replace(u'\u00BD', "0.5")