Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ignacio's answer is probably the best way to handle it, but if you aren't using Python 2.6, you can build a function that does things a little more simply without having to rely on regular expressions. Here is a simple and not very robust version I threw together:</p> <pre><code>def parse_fraction(fraction): def parse_part(part): sections = part.split('/') if len(sections) == 1: return float(sections[0]) return float(sections[0]) / float(sections[1]) return sum( parse_part(part) for part in fraction.split() ) </code></pre> <p>This obviously isn't perfect, because it will still accept input like <code>'2 1/2 1/2'</code>, which it would evaluate as <code>3</code>, since it essentially sums up a space delimited list of numbers while evaluating each as a fraction as necessary.</p> <p>if you do stick with the regular expression based solution, you should know that you can use a raw string to avoid having to double backslash everything. Essentially, you can write:</p> <pre><code>mixednum = re.compile(r"(\d+) (\d+)/(\d+)") </code></pre> <p>The <code>r</code> in front of the string tells Python to not evaluate special characters within the string, so you can write literal backslashes and they will be treated as such. Also note that you do not need to escape the slash since it is not a special character in Python regular expressions (because it is not used to mark the borders of the literal regexp like in many languages). the <code>re.IGNORECASE</code> flag also doesn't make a lot of sense here, since it only includes numeric entities in the regexp, and <code>re.DOTALL</code> is also meaningless, since you have no dots for it to apply to.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload