You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
csv.writer with QUOTE_NONE still requires non-emtpy quotechar and escapechar #132813
I'd like to output all quotes verbatim, without any escaping/quoting, but this fails:
importcsvw=csv.writer(open('test.tsv', 'w'), delimiter='\t', quoting=csv.QUOTE_NONE)
w.writerow(('"hello"', '"world"'))
# _csv.Error: need to escape, but no escapechar set
When I set escapechar='' or quotechar='', it also fails:
importcsvw=csv.writer(open('test.tsv', 'w'), delimiter='\t', quoting=csv.QUOTE_NONE, escapechar='')
w.writerow(('"hello"', '"world"'))
# TypeError: "escapechar" must be a 1-character stringimportcsvw=csv.writer(open('test.tsv', 'w'), delimiter='\t', quoting=csv.QUOTE_NONE, quotechar='')
w.writerow(('"hello"', '"world"'))
# TypeError: "quotechar" must be a 1-character string
I would suggest that under QUOTE_NONE, escaping of quotes should not be performed at all, or at least allow empty escapechar / quotechar
changed the title [-]`csv.witer` with `QUOTE_NONE` still requires non-emtpy `quotechar` and `escapechar`[/-][+]`csv.writer` with `QUOTE_NONE` still requires non-emtpy `quotechar` and `escapechar`[/+]on Apr 24, 2025
Instructs writer objects to never quote fields. When the current delimiter occurs in output data it is preceded by the current escapechar character. If escapechar is not set, the writer will raise Error if any characters that require escaping are encountered.
So, it aligns with the expected behavior, namely:
If escapechar is not set, the writer will raise Error if any characters that require escaping are encountered.
I'm converting this into a feature request but since 3.14 is almost feature-frozen, it will need to wait 3.15, unless we consider this as a bug, which I don't as it's documented as such.
I don't think we need to change the meaning of QUOTE_NONE. However, we should allow a verbatim output, namely something that considers the content of a field as being whatever between two delimiters. If someone needs to use a delimiter inside a field, they must already have quoted it inside the .csv.
Yes, but the name is very ambiguous as it's too close to QUOTE_NONE. We need a name that convene the fact that we don't do anything but that is different from QUOTE_NONE. We need to think of a better name or think of an alternative (e.g., we could keep QUOTE_NONE and ignore quotes that required escapes with another option). I mainly used the csv module with the default settings, so I didn't encounter this and I don't know if it's not too niche in this case (and if it doesn't excess the scope of the module itself).
If we want to reduce the number of options, we could simply use flags (namely QUOTE_NONE | QUOTE_IGNORE_ERRORS) something like that?
The documentation is not accurate. It only mentions the current delimiter, but the quoting is also needed for quotechar, escapechar and the line separator.
Activity
[-]`csv.witer` with `QUOTE_NONE` still requires non-emtpy `quotechar` and `escapechar`[/-][+]`csv.writer` with `QUOTE_NONE` still requires non-emtpy `quotechar` and `escapechar`[/+]picnixz commentedon Apr 24, 2025
The docs say:
So, it aligns with the expected behavior, namely:
I'm converting this into a feature request but since 3.14 is almost feature-frozen, it will need to wait 3.15, unless we consider this as a bug, which I don't as it's documented as such.
I don't think we need to change the meaning of QUOTE_NONE. However, we should allow a verbatim output, namely something that considers the content of a field as being whatever between two delimiters. If someone needs to use a delimiter inside a field, they must already have quoted it inside the
.csv.vadimkantorov commentedon Apr 24, 2025
Yeah, I expected that QUOTE_NONE means "do nothing about quoting / attempt to escape quotes", but apparently it's not so...
picnixz commentedon Apr 24, 2025
For that to happen, I think we need a new quoting strategy, something like
QUOTE_IGNOREbut this name is a bit confusing =/vadimkantorov commentedon Apr 24, 2025
new
QUOTE_IGNOREmight work! :)picnixz commentedon Apr 24, 2025
Yes, but the name is very ambiguous as it's too close to
QUOTE_NONE. We need a name that convene the fact that we don't do anything but that is different fromQUOTE_NONE. We need to think of a better name or think of an alternative (e.g., we could keepQUOTE_NONEand ignore quotes that required escapes with another option). I mainly used the csv module with the default settings, so I didn't encounter this and I don't know if it's not too niche in this case (and if it doesn't excess the scope of the module itself).If we want to reduce the number of options, we could simply use flags (namely
QUOTE_NONE | QUOTE_IGNORE_ERRORS) something like that?serhiy-storchaka commentedon Apr 30, 2025
Use
quotechar=None.The documentation is not accurate. It only mentions the current delimiter, but the quoting is also needed for quotechar, escapechar and the line separator.
serhiy-storchaka commentedon Apr 30, 2025
On other hand, there may be a bug in the code too. In other places, quotechar is ignored when quoting is QUOTE_NONE, but not here.
I'm working on this.
13 remaining items
pythongh-132813: Fix the csv documentation for quoting and escaping (p…
pythongh-132813: Fix the csv documentation for quoting and escaping (p…
[3.14] gh-132813: Fix the csv documentation for quoting and escaping (G…
[3.13] gh-132813: Fix the csv documentation for quoting and escaping (G…
hugovk commentedon Jun 30, 2025
Thanks all for the report and PRs, they've now been merged and backported.
pythongh-132813: Fix the csv documentation for quoting and escaping (p…
pythongh-132813: Improve error messages for incorrect types and value…
pythongh-132813: Fix the csv documentation for quoting and escaping (p…
pythongh-132813: Fix the csv documentation for quoting and escaping (p…
pythongh-132813: Improve error messages for incorrect types and value…
pythongh-132813: Fix the csv documentation for quoting and escaping (p…
pythongh-132813: Improve error messages for incorrect types and value…
pythongh-132813: Fix the csv documentation for quoting and escaping (p…