删除Python中两个字符之间的字符

时间:2012-09-15 18:11:59

标签: python string

我的输入字符串如下所示:

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

我希望输出看起来像这样:

1724741,24527465,14.00,14.35,14.00,14.25

我玩re.sub但仍然无法弄明白。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

csv module很好地处理了引用:

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

答案 1 :(得分:0)

一个非常糟糕的解决方案是使用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

请注意,这也会重新格式化浮点数。

编辑:由于您显然正在处理CSV文件和带有数千个分隔符的整数,因此可能会使用更清晰的解决方案

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

您需要将en_GB.UTF8替换为您的计算机支持的语言环境(并将逗号作为千位分隔符)。