Python regex: find and replace commas between quotation marks -
i have string,
line = '12/08/2013,3,"9,25",42:51,"3,08","12,9","13,9",159,170,"3,19",437,' and find , replace commas, between quotation marks, ":". looking results
line = '12/08/2013,3,9:25,42:51,3:08,12:9,13:9,159,170,3:19,437,' so far have been able match pattern with,
import re re.findall('(\"\d),(.+?\")', line) however, guess should using
re.compile(...something..., line) re.sub(':', line) does know how this? thanks, labjunky
>>> import re >>> line = '12/08/2013,3,"9,25",42:51,"3,08","12,9","13,9",159,170,"3,19",437,' >>> re.sub(r'"(\d+),(\d+)"', r'\1:\2', line) '12/08/2013,3,9:25,42:51,3:08,12:9,13:9,159,170,3:19,437,' \1, \2 refer matched groups.
non-regex solution:
>>> ''.join(x if % 2 == 0 else x.replace(',', ':') i, x in enumerate(line.split('"'))) '12/08/2013,3,9:25,42:51,3:08,12:9,13:9,159,170,3:19,437,'
Comments
Post a Comment