[英]Locale-indepenent string to float conversion in python
I need to convert string to float, but there can be different input string formats, such as '1234,5'
or '1234.5'
or '1 234,5'
or '1,234.5'
or whatever. 我需要将字符串转换为浮点型,但是可以有不同的输入字符串格式,例如
'1234,5'
或'1234.5'
或'1 234,5'
或'1,234.5'
等等。 And I can not change locale decimal pointer or thousands separator, because I may not know what data I will get in advance. 而且我无法更改语言环境的十进制指针或千位分隔符,因为我可能不知道我会提前获得什么数据。
Is there a way or method or library to parse and convert to float this kind of locale-specific values without knowing which locale is used? 是否有一种方法或方法或库可以在不知道使用哪种语言环境的情况下进行解析和转换以浮起此类特定于语言环境的值?
PS Is there any solution exists for the same problem with dates? PS对于日期相同的问题是否存在解决方案?
TIA. TIA。
You can make some assumptions on which character is the thousands separator and which is the decimal point. 您可以假设哪个字符是千位分隔符,哪个字符是小数点。 However, there is a case where you cannot know for sure what do do:
但是,在某些情况下,您无法确定该怎么做:
.
.
or ,
. ,
。 If it occurs more than once, the number does not have a decimal point and that character is the thousands separator 123.456
or 123,456
might be the number 123456
or 123.456
. 123.456
或123,456
可能是数字123456
或123.456
。 However, with a number like 123.45
- ie the number of digits after the potential thousands separator not being a multiple of three - you can assume that it's a decimal point. 123.45
这样的数字-即潜在的千位分隔符后面的位数不是三的倍数,您可以假定它是一个小数点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.