user1642513
user1642513

Reputation:

Remove character between two characters in Python

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.

Upvotes: 2

Views: 300

Answers (2)

Sven Marnach
Sven Marnach

Reputation: 601799

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).

Upvotes: 0

Janne Karila
Janne Karila

Reputation: 25197

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

Upvotes: 4

Related Questions