Search code examples
pythonpoints

Points for thousand mark and decimal mark


I have a messed up txt-file, with points as a thousand mark (1.000 or 19.329) and as a decimal mark (10000.3). Two example lines:

John;1.952;2003;20.365;1.214 

Ryan;2.342;2002;3045.3;345

I want to remove the point for the thousand mark and keep the points for the decimals. What is the easiest way to do this?


Solution

  • If you never have exactly three decimal places after the decimal point, the following will do it:

    >>> import re
    >>> re.sub(r"\.(\d\d\d(\D|$))", r"\1", "200.000.5")
    '200000.5'
    

    The regexp removes a dot if it is followed by exactly three digits. It won't match fewer digits (since it looks for three \d), and it won't match more since it looks for a non-digit after them (\D).