Include tab as a supported delimiting character for auto-detection

This commit is contained in:
Jeremy Stretch 2023-09-05 13:59:44 -04:00
parent cce89a55fb
commit f14f2fa5f0

View File

@ -105,18 +105,16 @@ 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
if delimiter == CSVDelimiterChoices.AUTO: if delimiter == CSVDelimiterChoices.AUTO:
# Determine the CSV dialect # This uses a rough heuristic to detect the CSV dialect based on the presence of supported delimiting
# characters. If the data is malformed, we'll fall back to the default Excel dialect.
delimiters = ''.join(CSVDelimiterChoices.values()[1:]) # Skip "auto"
try: try:
# This uses a rough heuristic to detect the CSV dialect. If the data is malformed, we'll fall back to dialect = csv.Sniffer().sniff(data.strip(), delimiters=delimiters)
# the default Excel dialect. Note that delimiter can only be one character.
dialect = csv.Sniffer().sniff(
data.strip(), delimiters=''.join([CSVDelimiterChoices.COMMA, CSVDelimiterChoices.SEMICOLON])
)
except csv.Error: except csv.Error:
dialect = csv.excel dialect = csv.excel
elif delimiter in [CSVDelimiterChoices.COMMA, CSVDelimiterChoices.SEMICOLON]: elif delimiter in (CSVDelimiterChoices.COMMA, CSVDelimiterChoices.SEMICOLON):
dialect = csv.excel dialect = csv.excel
dialect.delimiter = delimiter dialect.delimiter = delimiter
elif delimiter == CSVDelimiterChoices.TAB: elif delimiter == CSVDelimiterChoices.TAB: