CSV reader ignore brackets

Piet van Oostrum piet-l at vanoostrum.org
Thu Sep 26 11:54:45 EDT 2019


Skip Montanaro <skip.montanaro at gmail.com> writes:

> How about just replacing *\(([^)]*)\)* with *"\1"* in a wrapper class's
> line reading method? (I think I have the re syntax approximately right.)
> The csv reader will "just work". Again, nesting parens not allowed.
>
> Skip

here is some working code:

def PReader(csvfile):
    import re
    for line in csvfile:
        line = re.sub(r'\(.*?\)', '"\g<0>"', line)
        yield line

import csv
with open('testcsv.csv') as csvfile:
    reader = csv.reader(PReader(csvfile), quotechar='"')
    for row in reader:
        print(row)


-- 
Piet van Oostrum <piet-l at vanoostrum.org>
WWW: http://piet.vanoostrum.org/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list