Skip to content

csv writer with blank lineterminator breaks quoting #67044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
EricHaszlakiewicz mannequin opened this issue Nov 12, 2014 · 9 comments
Closed

csv writer with blank lineterminator breaks quoting #67044

EricHaszlakiewicz mannequin opened this issue Nov 12, 2014 · 9 comments
Labels
docs Documentation in the Doc dir easy type-bug An unexpected behavior, bug, or error

Comments

@EricHaszlakiewicz
Copy link
Mannequin

EricHaszlakiewicz mannequin commented Nov 12, 2014

BPO 22855
Nosy @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2014-11-12.17:05:03.785>
labels = ['type-bug', 'docs']
title = 'csv writer with blank lineterminator breaks quoting'
updated_at = <Date 2014-11-12.17:37:50.605>
user = 'https://bugs.python.org/EricHaszlakiewicz'

bugs.python.org fields:

activity = <Date 2014-11-12.17:37:50.605>
actor = 'r.david.murray'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2014-11-12.17:05:03.785>
creator = 'Eric.Haszlakiewicz'
dependencies = []
files = []
hgrepos = []
issue_num = 22855
keywords = []
message_count = 3.0
messages = ['231083', '231086', '231088']
nosy_count = 3.0
nosy_names = ['r.david.murray', 'docs@python', 'Eric.Haszlakiewicz']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue22855'
versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

Linked PRs

@EricHaszlakiewicz
Copy link
Mannequin Author

EricHaszlakiewicz mannequin commented Nov 12, 2014

I'm trying to emit a single line of csv without any line terminators, but specifying lineterminator=None results in a "lineterminator must be set" error, and setting lineterminator='' results in lack of quotes around certain fields.

    with open("foo.csv", "wb") as csvfile:
        csvw = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL, lineterminator='')
        csvw.writerow(["col1","col2\ndata", "col3"])

I expected the contents of the file to be:
col1,"col2
data",col3

It should be possible to change the lineterminator without changing the quoting behavior.
At the very least, the documentation needs to explain better what logic is used to determine whether something gets quoted, and what affects that.

@EricHaszlakiewicz EricHaszlakiewicz mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 12, 2014
@bitdancer
Copy link
Member

If the line terminator is not \n, there is no reason to quote values with \n in them. (Try your code with lineterminator set to 'd' to see what I mean.)

@bitdancer
Copy link
Member

Also, it is hard to see how to make this clearer:

csv.QUOTE_MINIMAL
Instructs writer objects to only quote those fields which contain special characters such as delimiter, quotechar or any of the characters in lineterminator.

Hmm. Perhaps it would be a bit clearer if it said "... which contain the special characters delimiter, quotechar, ..."

@bitdancer bitdancer added docs Documentation in the Doc dir and removed stdlib Python modules in the Lib dir labels Nov 12, 2014
@bitdancer bitdancer reopened this Nov 12, 2014
@bitdancer bitdancer removed the invalid label Nov 12, 2014
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@slateny slateny added the easy label Aug 23, 2022
@serhiy-storchaka
Copy link
Member

Note that quotechar is only quoted if doublequote is true.

@serhiy-storchaka
Copy link
Member

The reader does not support non-quoted and non-escaped \r and \n, independently from lineterminator. So they should always be quoted or escaped.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 20, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 23, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 23, 2024
serhiy-storchaka added a commit that referenced this issue Feb 23, 2024
smontanaro pushed a commit to smontanaro/cpython that referenced this issue Feb 23, 2024
…() (pythonGH-115741)

(cherry picked from commit c688c0f)

Co-authored-by: Serhiy Storchaka <[email protected]>
serhiy-storchaka added a commit that referenced this issue Feb 23, 2024
@aterrel
Copy link

aterrel commented May 21, 2025

I think this is expected behaviour. There is certainly a quirk around escaping and quoting.

The writer hits \n, but with no escapechar set it quotes the element. If the user escapes the \n for there is no quoting.

>>> with open('file2.csv', 'w') as csvfile:
...     csvw = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL, lineterminator='')
...     csvw.writerow(["col1","col2\\ndata", "col3"])
...  
$ cat file2.csv 
col1,col2\ndata,col3%   

If one sets the quoting policy to be csv.QUOTE_NONE then an error on the original code reveals that it cannot escape the \n. But if one doesn't want the quoting, one can escape with csv.QUOTE_NONE

>>> with open('file3.csv', 'w') as csvfile:
...     csvw = csv.writer(csvfile, quoting=csv.QUOTE_NONE, lineterminator='')
...     csvw.writerow(["col1","col2\\ndata", "col3"])
$ cat file2.csv 
col1,col2\ndata,col3%   

Unfortunately if one does set a escapechar the quirky behavior emerges:

>>> import csv
... with open('file4.csv', 'w') as csvfile:
...     csvw = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL, lineterminator='', escapechar='\\')
...     csvw.writerow(["col1","col2\ndata", "col3"])
$ cat  file4.csv
col1,"col2
data",col3%                                                                                                                                                                                   

@serhiy-storchaka
Copy link
Member

This is expected. Quoting is used if possible. Setting quoting=csv.QUOTE_NONE or quotechar=None disables quoting, so escaping is used for quoting char.

>>> import csv, sys
>>> csvw = csv.writer(sys.stdout, quoting=csv.QUOTE_NONE, lineterminator='', escapechar='\\')
>>> csvw.writerow(["col1","col2\ndata", "col3"])
col1,col2\
data,col320
>>> csvw = csv.writer(sys.stdout, quotechar=None, lineterminator='', escapechar='\\')
>>> csvw.writerow(["col1","col2\ndata", "col3"])
col1,col2\
data,col320

@aterrel
Copy link

aterrel commented May 21, 2025

@serhiy-storchaka so close this bug as "wont-fix"?

@serhiy-storchaka
Copy link
Member

Actually, it was fixed by #115741

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir easy type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

4 participants