Reputation: 96
I'd like to test bad CSV input data but I'm not able to simulate it.
my code is:
dialect = csv.Sniffer().sniff(fd.readline(), delimiters=',;')
dialect.strict = True
fd.seek(0)
reader = csv.DictReader(fd, dialect=dialect)
for row in reader:
print(row)
There is no difference if I use dialect.strict = True or dialect.strict = False on any my data files. Can someone show me CSV data raising Error when dialect.strict = True and no exception when dialect.strict = False?
Python doc for a reference: https://docs.python.org/3/library/csv.html#csv.Dialect.strict
Upvotes: 0
Views: 1015
Reputation: 106455
Setting dialect.strict = True
would make the reader raise an exception when the input data does not have a matching ending quote.
For example:
import csv
from io import StringIO
fd = StringIO('''a,b
x,"y''')
dialect = csv.Sniffer().sniff(fd.readline(), delimiters=',;')
dialect.strict = False
fd.seek(0)
reader = csv.DictReader(fd, dialect=dialect)
for row in reader:
print(row)
would output:
{'a': 'x', 'b': 'y'}
But setting dialect.strict = True
would produce:
_csv.Error: unexpected end of data
Upvotes: 1