2

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

"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,但仍然无法弄清楚。任何帮助,将不胜感激。

4

2 回答 2

4

csv 模块很好地处理了引用:

>>> 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
于 2012-09-15T18:19:58.290 回答
0

一个非常hacky的解决方案是使用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用您的机器支持的语言环境进行替换(并使用逗号作为千位分隔符)。

于 2012-09-15T18:15:36.030 回答