You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
csv.Sniffer returns a dialect that drops empty space-delimited fields #161400
With a space as the delimiter, csv.Sniffer.sniff() returns skipinitialspace=True for a sample with an unquoted empty field when one other row is shorter, so the sniffed dialect loses the empty field:
importcsv, iodata= ('name age city\n''alice 30 paris\n''bob london\n''carol 25 "new york"\n''dave 40 berlin\n''erin 35\n')
dialect=csv.Sniffer().sniff(data)
print(dialect.skipinitialspace, list(csv.reader(io.StringIO(data), dialect))[2])
On main:
True ['bob', 'london']
On 3.15.0:
False ['bob', '', 'london']
The shorter row can also be the last row of a cut-off sample, as in the documented sniff(csvfile.read(1024)): with the last row 'erin 35 rome\n', sniffing data[:-6] gives the same result. The docs say sniff() picks the combination "with the most consistent number of fields", which here is skipinitialspace=False. This came with gh-154744.
Activity