Incorrect import CSV with multiline values #5632

Closed
opened 2025-12-29 19:30:20 +01:00 by adam · 2 comments
Owner

Originally created by @seros1521 on GitHub (Nov 9, 2021).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.0.8

Python version

3.7

Steps to Reproduce

1.open the CSV file import page for Sites
2.import a CSV file with the following content:

name,slug,status,comments
name1,slug1,active,"multiline
comment"

Expected Behavior

The "comments" field is expected to preserve the line break.

Observed Behavior

When importing a file, the line break in the multi-line comments field disappears.
When importing through the "CSV Data" form, the import is correct, the line feed is preserved.

Originally created by @seros1521 on GitHub (Nov 9, 2021). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.0.8 ### Python version 3.7 ### Steps to Reproduce 1.open the CSV file import page for Sites 2.import a CSV file with the following content: ``` name,slug,status,comments name1,slug1,active,"multiline comment" ``` ### Expected Behavior The "comments" field is expected to preserve the line break. ### Observed Behavior When importing a file, the line break in the multi-line comments field disappears. When importing through the "CSV Data" form, the import is correct, the line feed is preserved.
adam added the type: bugstatus: accepted labels 2025-12-29 19:30:20 +01:00
adam closed this issue 2025-12-29 19:30:20 +01:00
Author
Owner

@seros1521 commented on GitHub (Nov 9, 2021):

Different behavior occurs due to different way of processing input data:
26ceeb61ef/netbox/utilities/forms/fields.py (L190)
and
26ceeb61ef/netbox/utilities/forms/fields.py (L227)

Demo:

>>> import csv
>>> from io import StringIO
>>> s="""name,slug,status,comments
... name1,slug1,active,"multiline
... comment"
... """
>>> 
>>> [i for i in s.splitlines()]
['name,slug,status,comments', 'name1,slug1,active,"multiline', 'comment"']
>>> 
>>> [i for i in StringIO(s)]
['name,slug,status,comments\n', 'name1,slug1,active,"multiline\n', 'comment"\n']
>>> 
>>> [i for i in csv.reader(s.splitlines())]
[['name', 'slug', 'status', 'comments'], ['name1', 'slug1', 'active', 'multilinecomment']]
>>> 
>>> [i for i in csv.reader(StringIO(s))]
[['name', 'slug', 'status', 'comments'], ['name1', 'slug1', 'active', 'multiline\ncomment']]
>>> 

@seros1521 commented on GitHub (Nov 9, 2021): Different behavior occurs due to different way of processing input data: https://github.com/netbox-community/netbox/blob/26ceeb61ef9bca7d348caa71b7353a279f70c775/netbox/utilities/forms/fields.py#L190 and https://github.com/netbox-community/netbox/blob/26ceeb61ef9bca7d348caa71b7353a279f70c775/netbox/utilities/forms/fields.py#L227 Demo: ``` >>> import csv >>> from io import StringIO >>> s="""name,slug,status,comments ... name1,slug1,active,"multiline ... comment" ... """ >>> >>> [i for i in s.splitlines()] ['name,slug,status,comments', 'name1,slug1,active,"multiline', 'comment"'] >>> >>> [i for i in StringIO(s)] ['name,slug,status,comments\n', 'name1,slug1,active,"multiline\n', 'comment"\n'] >>> >>> [i for i in csv.reader(s.splitlines())] [['name', 'slug', 'status', 'comments'], ['name1', 'slug1', 'active', 'multilinecomment']] >>> >>> [i for i in csv.reader(StringIO(s))] [['name', 'slug', 'status', 'comments'], ['name1', 'slug1', 'active', 'multiline\ncomment']] >>> ```
Author
Owner

@DanSheps commented on GitHub (Nov 9, 2021):

Can you comment your test cases?

To clarify, StringIO is the desired import right?

When importing a file, the line break in the multi-line comments field disappears.
When importing through the "CSV Data" form, the import is correct, the line feed is preserved.

Could you also clarify this? What is the case for each?

@DanSheps commented on GitHub (Nov 9, 2021): Can you comment your test cases? To clarify, StringIO is the desired import right? > When importing a file, the line break in the multi-line comments field disappears. > When importing through the "CSV Data" form, the import is correct, the line feed is preserved. Could you also clarify this? What is the case for each?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5632