如何将unicode字符转换为Python中的浮点数? [英] How do I convert unicode characters to floats in Python?

查看:564
本文介绍了如何将unicode字符转换为Python中的浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一个网页,它有分数的Unicode表示。我希望能够直接把这些字符串,并将其转换为浮动。例如:

⅕将变成0.2



有关如何在Python中执行此操作的任何建议? / p>

解决方案

您想使用

  import unicodedata 
unicodedata.numeric(u'⅕ ')

这将会打印:

 0.20000000000000001 

如果该字符没有数字值, unicodedata.numeric(unichr [,默认值])将会返回默认值,或者如果没有给出默认值,将会引发ValueError


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?

解决方案

You want to use the unicodedata module:

import unicodedata
unicodedata.numeric(u'⅕')

This will print:

0.20000000000000001

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.

这篇关于如何将unicode字符转换为Python中的浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆