我有一个问题,我可以将1/2转换为0.5,但我不知道如何将1/2转换为1.5或2/2转换为2.5。This is the link。这里有1/5 kg的行,我想如果你能给我看一个更高级的replace()参数或者别的什么的话,它会对我有所帮助。这是我已经必须转换为0.5的代码:
raw_string = re.sub(r'\s{2,}', ' ', ingredient.get_text(strip=True))
raw_string = raw_string.replace(u'\u00BD', "0.5")发布于 2020-06-10 16:46:50
一个可行的解决方案:
# 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") https://stackoverflow.com/questions/62299293
复制相似问题