1. 正则提取

  1. import re
  2. indredient = 'Kumquat: 2 cups'
  3. pattern_text = r'(?P<indredient>\w+):\s+(?P<amount>\d+)\s+(?P<unit>\w+)'
  4. p = re.compile(pattern_text)
  5. m = p.match(indredient)
  6. indredient=m.group('indredient')
  7. amount=m.group('amount')
  8. unit=m.group('unit')
  9. (indredient,amount,unit)=m.groups()