ardabro
ardabro

Reputation: 2061

How to parse csv with quoted strings - advanced case

I'm trying to make csv module to parse lines containing quoted strings and quoted separators. Unfortunately I'm not able to achieve desired results with any dialect/format parameters. Is there any way to parse this:

'"AAA", BBB, "CCC, CCC"'

and get this:

['"AAA"', 'BBB', '"CCC, CCC"']    # 3 elements, one quoted separator

?

Two fundamental requirements:

  1. Quotations have to be preserved
  2. Quoted, and not escaped separators have to be copied as regular characters

Is it possible?

Upvotes: 3

Views: 978

Answers (2)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

There are 2 issues to overcome:

That second part is described in the documentation as:

Dialect.doublequote

Controls how instances of quotechar appearing inside a field should themselves be quoted. When True, the character is doubled. When False, the escapechar is used as a prefix to the quotechar. It defaults to True.

standalone example, without file:

import csv
data = ['"AAA", BBB, "CCC, CCC"'.replace('"','"""')]

cr = csv.reader(data,skipinitialspace=True)
row = next(cr)
print(row)

result:

['"AAA"', 'BBB', '"CCC, CCC"']

with a file as input:

import csv
with open("input.csv") as f:
  cr = csv.reader((l.replace('"','"""' for l in f),skipinitialspace=True)
  for row in cr:
     print(row)

Upvotes: 4

Zeeshan Khan
Zeeshan Khan

Reputation: 53

Have you tried this ?

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

Upvotes: 0

Related Questions