Skip to content

csv.writer with QUOTE_NONE still requires non-emtpy quotechar and escapechar #132813

Closed

Description

@vadimkantorov

Bug report

Bug description:

I'd like to output all quotes verbatim, without any escaping/quoting, but this fails:

import csv
w = 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:

import csv
w = csv.writer(open('test.tsv', 'w'), delimiter = '\t', quoting = csv.QUOTE_NONE, escapechar = '')
w.writerow(('"hello"', '"world"'))
# TypeError: "escapechar" must be a 1-character string

import csv
w = 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


The workaround / hack I found:

import csv
w = csv.writer(open('test.tsv', 'w'), delimiter = '\t', quoting = csv.QUOTE_NONE, quotechar = '\t')
w.writerow(('"hello"', '"world"'))

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Apr 24, 2025
  2. 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
  3. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Apr 24, 2025
  4. picnixz commented on Apr 24, 2025

    @picnixz
    Member

    The docs say:

    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.

  5. vadimkantorov commented on Apr 24, 2025

    @vadimkantorov
    Author

    Yeah, I expected that QUOTE_NONE means "do nothing about quoting / attempt to escape quotes", but apparently it's not so...

  6. picnixz commented on Apr 24, 2025

    @picnixz
    Member

    For that to happen, I think we need a new quoting strategy, something like QUOTE_IGNORE but this name is a bit confusing =/

  7. vadimkantorov commented on Apr 24, 2025

    @vadimkantorov
    Author

    new QUOTE_IGNORE might work! :)

  8. picnixz commented on Apr 24, 2025

    @picnixz
    Member

    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?

  9. serhiy-storchaka commented on Apr 30, 2025

    @serhiy-storchaka
    Member

    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.

  10. serhiy-storchaka commented on Apr 30, 2025

    @serhiy-storchaka
    Member

    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.

  11. 13 remaining items

  12. added 2 commits that reference this issue on Jun 30, 2025
  13. added 2 commits that reference this issue on Jun 30, 2025
  14. hugovk commented on Jun 30, 2025

    @hugovk
    Member

    Thanks all for the report and PRs, they've now been merged and backported.

  15. moved this from In Progress to Done in CSV issues 📊on Jun 30, 2025
  16. added a commit that references this issue on Jul 11, 2025
  17. added 2 commits that reference this issue on Jul 12, 2025
  18. added a commit that references this issue on Jul 13, 2025
  19. added 2 commits that reference this issue on Aug 4, 2025
  20. added 2 commits that reference this issue on Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

extension-modulesC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions