Python 货币字符串转换为浮点数

问题描述 投票:0回答:0

这是名单:

x = ["111,222","111.222","111,222.11","111.222,11","111111","111.22"]

我希望它能在不使用语言环境的情况下正确转换,并且能与上面的集合一起工作,因为原始数据集是一团糟

我试过了

  • 正则表达式

  • float("".join(["".join([i for i in list(s)[0:-3] if i not in [".",","]]),"".join(list(s)[-3:]).replace(",",".")]) if list(s)[-3] in [".",","] else "".join(list(s)))

  • r = re.sub('[^0-9]', '', s) export = float(r[0:-2]+'.'+r[-2:])

  •    comma = ","
       dot = "."
    
       last_comma_index = s.rfind(comma)
       last_dot_index = s.rfind(dot)
    
       if last_comma_index > last_dot_index:
           last_index = last_comma_index
       else:
           last_index = last_dot_index
    
       before_point = s[:last_index]
    
       no_commas = "".join(before_point.split(comma))
       no_dots = "".join(no_commas.split(dot))
    
       export  = no_dots + dot + s[last_index + 1:]
    
  •        s = "".join(c for c in s if c.isdigit() or c in [",", "."])
    
           if "," in s:
               decimal_sep = ","
               thousands_sep = "."
           else:
               decimal_sep = "."
               thousands_sep = ","
    
           s = s.replace(decimal_sep, ".")
           s = re.sub(f"\\{thousands_sep}(?=[0-9])", "", s)
           export = s
    
  •        def parseNumber(text):
    
           try:
               # First we return None if we don't have something in the text:
               if text is None:
                   return None
               if isinstance(text, int) or isinstance(text, float):
                   return text
               text = text.strip()
               if text == "":
                   return None
               # Next we get the first "[0-9,. ]+":
               n = re.search("-?[0-9]*([,. ]?[0-9]+)+", text).group(0)
               n = n.strip()
               if not re.match(".*[0-9]+.*", text):
                   return None
               # Then we cut to keep only 2 symbols:
               while " " in n and "," in n and "." in n:
                   index = max(n.rfind(','), n.rfind(' '), n.rfind('.'))
                   n = n[0:index]
               n = n.strip()
               # We count the number of symbols:
               symbolsCount = 0
               for current in [" ", ",", "."]:
                   if current in n:
                       symbolsCount += 1
               # If we don't have any symbol, we do nothing:
               if symbolsCount == 0:
                   pass
               # With one symbol:
               elif symbolsCount == 1:
                   # If this is a space, we just remove all:
                   if " " in n:
                       n = n.replace(" ", "")
                   # Else we set it as a "." if one occurence, or remove it:
                   else:
                       theSymbol = "," if "," in n else "."
                       if n.count(theSymbol) > 1:
                           n = n.replace(theSymbol, "")
                       else:
                           n = n.replace(theSymbol, ".")
               else:
                   # Now replace symbols so the right symbol is "." and all left are "":
                   rightSymbolIndex = max(n.rfind(','), n.rfind(' '), n.rfind('.'))
                   rightSymbol = n[rightSymbolIndex:rightSymbolIndex+1]
                   if rightSymbol == " ":
                       return parseNumber(n.replace(" ", "_"))
                   n = n.replace(rightSymbol, "R")
                   leftSymbolIndex = max(n.rfind(','), n.rfind(' '), n.rfind('.'))
                   leftSymbol = n[leftSymbolIndex:leftSymbolIndex+1]
                   n = n.replace(leftSymbol, "L")
                   n = n.replace("L", "")
                   n = n.replace("R", ".")
               # And we cast the text to float or int:
               n = float(n)
    
               if n > 5000000:
                   return 0
               elif n.is_integer():
                   return int(n)
               else:
                   return n
    
           except: pass
    
           return None
    
  • 十进制

  • 现场

    • StackOverflow 中的大多数答案都在语言环境附近,但由于数据混乱,必须避免它...

结果应该是这样的:

x = [111222,111222,111222.11,111222.11,111111,111.22]

期待任何建议。

python currency
© www.soinside.com 2019 - 2024. All rights reserved.