简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM