From f14f2fa5f0e2076d626db3578d707e06cdd55372 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 5 Sep 2023 13:59:44 -0400 Subject: [PATCH] Include tab as a supported delimiting character for auto-detection --- netbox/utilities/forms/bulk_import.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/netbox/utilities/forms/bulk_import.py b/netbox/utilities/forms/bulk_import.py index 99952a1be..6c736ae29 100644 --- a/netbox/utilities/forms/bulk_import.py +++ b/netbox/utilities/forms/bulk_import.py @@ -105,18 +105,16 @@ class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form): """ Clean CSV-formatted data. The first row will be treated as column headers. """ - + # Determine the CSV dialect 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: - # This uses a rough heuristic to detect the CSV dialect. If the data is malformed, we'll fall back to - # the default Excel dialect. Note that delimiter can only be one character. - dialect = csv.Sniffer().sniff( - data.strip(), delimiters=''.join([CSVDelimiterChoices.COMMA, CSVDelimiterChoices.SEMICOLON]) - ) + dialect = csv.Sniffer().sniff(data.strip(), delimiters=delimiters) except csv.Error: dialect = csv.excel - elif delimiter in [CSVDelimiterChoices.COMMA, CSVDelimiterChoices.SEMICOLON]: + elif delimiter in (CSVDelimiterChoices.COMMA, CSVDelimiterChoices.SEMICOLON): dialect = csv.excel dialect.delimiter = delimiter elif delimiter == CSVDelimiterChoices.TAB: