2

My input string looks like this:

"1,724,741","24,527,465",14.00,14.35,14.00,14.25

I want the output to look like this:

1724741,24527465,14.00,14.35,14.00,14.25

I played with re.sub but still couldn't figure out. Any help would be appreciated.

2 Answers 2

4

The csv module handles the quoting nicely:

>>> s = '"1,724,741","24,527,465",14.00,14.35,14.00,14.25'
>>> import csv
>>> r = csv.reader([s])
>>> for row in r:
...     print ','.join(x.replace(",", "") for x in row)
... 
1724741,24527465,14.00,14.35,14.00,14.25
Sign up to request clarification or add additional context in comments.

Comments

0

A quite hacky solution is to use ast.literal_eval():

>>> from ast import literal_eval
>>> s = '"1,724,741","24,527,465",14.00,14.35,14.00,14.25'
>>> print ",".join(x.replace(",", "") if isinstance(x, str) else str(x)
...                for x in literal_eval(s))
1724741,24527465,14.0,14.35,14.0,14.25

Note that this also reformats the floating point numbers.

Edit: Since you are apparently dealing with a CSV file and integers with thousands separators, a cleaner solution might be

import csv
import locale

locale.setlocale(locale.LC_ALL, 'en_GB.UTF8')
converters = [locale.atoi] * 2 + [locale.atof] * 4
with open("input.csv", "rb") as f, open("output.csv", "wb") as g:
    out = csv.writer(g)
    for row in csv.reader(f):
        out.writerow([conv(x) for conv, x in zip(converters, row)])

You will need to substitute en_GB.UTF8 by a locale supported by your machine (and having comma as a thousands separator).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.