Importing csv exports breaks if a combination of quotes and line breaks are used in the comment field #3366

Closed
opened 2025-12-29 18:28:22 +01:00 by adam · 1 comment
Owner

Originally created by @dare-to-recurse on GitHub (Feb 21, 2020).

Environment

  • Python version: 3.6.8
  • NetBox version: 2.7.7

Steps to Reproduce

  1. Create site "Demo site" with the following in the comment field:
This is "broken" due to quotes in comment content
Some additional text on a new line.
  1. Export sites to a csv file using the Export button
  2. Delete "Demo site" from NetBox
  3. Open page for importing sites
  4. Copy the content of the exported file into the import form for sites.
  5. Click Submit button

Expected Behavior

Import to succeed and "Demo site" to be created again with the same information as what was exported.

Observed Behavior

Import fails with message "Row 2: Expected 17 columns but found 1"

Originally created by @dare-to-recurse on GitHub (Feb 21, 2020). ### Environment * Python version: 3.6.8 * NetBox version: 2.7.7 ### Steps to Reproduce 1. Create site "Demo site" with the following in the comment field: ``` This is "broken" due to quotes in comment content Some additional text on a new line. ``` 3. Export sites to a csv file using the Export button 4. Delete "Demo site" from NetBox 5. Open page for importing sites 6. Copy the content of the exported file into the import form for sites. 7. Click Submit button ### Expected Behavior Import to succeed and "Demo site" to be created again with the same information as what was exported. ### Observed Behavior Import fails with message "Row 2: Expected 17 columns but found 1"
adam added the type: bugstatus: accepted labels 2025-12-29 18:28:22 +01:00
adam closed this issue 2025-12-29 18:28:22 +01:00
Author
Owner

@hSaria commented on GitHub (Feb 21, 2020):

I think we need to replace every instance of " with "" when exporting, as per 2.7. in RFC4180. So something like this would do it:

diff --git a/netbox/utilities/utils.py b/netbox/utilities/utils.py
index 6969a60e..83442566 100644
--- a/netbox/utilities/utils.py
+++ b/netbox/utilities/utils.py
@@ -31,9 +31,9 @@ def csv_format(data):
         if not isinstance(value, str):
             value = '{}'.format(value)
 
-        # Double-quote the value if it contains a comma
-        if ',' in value or '\n' in value:
-            csv.append('"{}"'.format(value))
+        # Double-quote the value if it contains a comma and replace double-quotes in the data with 2x double-quotes.
+        if ',' in value or '\n' in value or '"' in value:
+            csv.append('"{}"'.format(value.replace('"', '""')))
         else:
             csv.append('{}'.format(value))
@hSaria commented on GitHub (Feb 21, 2020): I think we need to replace every instance of `"` with `""` when exporting, as per 2.7. in [RFC4180](https://tools.ietf.org/html/rfc4180#page-3). So something like this would do it: ```diff diff --git a/netbox/utilities/utils.py b/netbox/utilities/utils.py index 6969a60e..83442566 100644 --- a/netbox/utilities/utils.py +++ b/netbox/utilities/utils.py @@ -31,9 +31,9 @@ def csv_format(data): if not isinstance(value, str): value = '{}'.format(value) - # Double-quote the value if it contains a comma - if ',' in value or '\n' in value: - csv.append('"{}"'.format(value)) + # Double-quote the value if it contains a comma and replace double-quotes in the data with 2x double-quotes. + if ',' in value or '\n' in value or '"' in value: + csv.append('"{}"'.format(value.replace('"', '""'))) else: csv.append('{}'.format(value)) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3366