I want to turn a string like "$1,234.56" into 1234.56 so I can add it to a database, my current solution is using re.sub("[^0-9]", "", str1
this however strips the punctuation too, is there a way to keep it?
I'm using python 3.6.5
I want to turn a string like "$1,234.56" into 1234.56 so I can add it to a database, my current solution is using re.sub("[^0-9]", "", str1
this however strips the punctuation too, is there a way to keep it?
I'm using python 3.6.5
re.sub("[^0-9.]", "", str1)
will keep the decimal dot too. Is there any other punctuation you want to keep?
Note that this will still fail to produce a valid integer on inputs with more than one decimal dot, such as 123..45
or 1.2.3
or .
or 1.
or anything that looks weird.
The code discards everything but numbers and points, with no regard as to how many points or numbers are there. An infeasible number such that 223523523948231472389523
will still be kept, and depending on the way your database works (whether or not it can store numbers above 64 bits), the resulting number stored in the database may be negative or truncated!