adds csv dialect detection to bulk import view #13239

This commit is contained in:
Abhimanyu Saharan 2023-08-25 17:46:22 +05:30
parent ca5e69897d
commit e782077bb7

View File

@ -90,8 +90,17 @@ class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
""" """
Clean CSV-formatted data. The first row will be treated as column headers. Clean CSV-formatted data. The first row will be treated as column headers.
""" """
# Determine the CSV dialect
try:
# This uses a rough heuristic to detect the CSV dialect. If the data is malformed, we'll fall back to
# the default Excel dialect.
dialect = csv.Sniffer().sniff(data.strip())
except csv.Error:
dialect = csv.excel
stream = StringIO(data.strip()) stream = StringIO(data.strip())
reader = csv.reader(stream) reader = csv.reader(stream, dialect=dialect)
headers, records = parse_csv(reader) headers, records = parse_csv(reader)
# Set CSV headers for reference by the model form # Set CSV headers for reference by the model form