Paul Paul - 1 year ago 96
Python Question

How do I convert unicode characters to floats in Python?

I am parsing a webpage which has Unicode representations of fractions. I would like to be able to take those strings directly and convert them to floats. For example:

"⅕" would become 0.2

Any suggestions of how to do this in Python?

Answer Source

You want to use the unicodedata module:

import unicodedata

This will print:


If the character does not have a numeric value, then unicodedata.numeric(unichr[, default]) will return default, or if default is not given will raise ValueError.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download