简体   繁体   English

独立于语言环境的字符串在python中进行浮点转换

[英]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: 但是,在某些情况下,您无法确定该怎么做:

  • Look for the last character that is . 寻找最后一个字符. or , . 或者, If it occurs more than once, the number does not have a decimal point and that character is the thousands separator 如果出现多次,则该数字没有小数点,并且该字符是千位分隔符
  • If the string contains exactly one of each, the last one is the decimal point 如果字符串恰好包含每个字符串,则最后一个是小数点
  • If the string contains only one point/comma, you are pretty much out of luck: 123.456 or 123,456 might be the number 123456 or 123.456 . 如果字符串仅包含一个点/逗号,那么您很不走运: 123.456123,456可能是数字123456123.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM