GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Feature request: sniff csv quoting and lineterminator · Issue #129374 · python/cpython

Feature request: sniff csv quoting and lineterminator #129374

New issue
Copy link
New issue
Copy link
Open
Open
Feature request: sniff csv quoting and lineterminator#129374
Copy link
Labels
3.16new features, bugs and security fixesnew features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@Nikilauz
Nikilauz
opened on Jan 27, 2025
Issue body actions

If you read a csv file, alter the data and then write it back, it would be very nice to recreate also the quoting behaviour. So e.g. if the file is quoted in style csv.QUOTE_NONNUMERIC, it would be very useful to have the quoting property set correctly in the dialect object returned by csv.Sniffer().sniff.
Also, I just figured out that the line terminator is also hard-coded to "\r\n". Would also be nice to be adaptive...

Linked PRs

  • gh-129374: adds quoting detection to csv sniffer #134416

Activity

  1. Nikilauz
    changed the title [-]Feature request: sniff csv quoting[/-] [+]Feature request: sniff csv quoting and lineterminator[/+] on Jan 27, 2025
  2. tomasr8
    added this to CSV issues 📊on Jan 27, 2025
  3. tomasr8
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jan 27, 2025
  4. encukou
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Jan 28, 2025
  5. ericvsmith commented on Jan 28, 2025

    @ericvsmith
    ericvsmith
    on Jan 28, 2025
    Member
    More actions

    This probably belongs on discuss.python.org. But in any event, you'll need to specify more information. What specific changes would you like to see?

  6. Nikilauz commented on Feb 3, 2025

    @Nikilauz
    Nikilauz
    on Feb 3, 2025
    Author
    More actions

    In general, I would expect the Dialect object returned by csv.Sniffer.sniff to have its properties set in a way that a writer object initialized with the dialect would reproduce a similar csv string. This seems to be a clear correctness constraint for the dialect functionality for me, so is this really something to put in a discussion forum? I'm right aware that this may be an issue with a questionable benefit-cost-ratio, though...
    Anyhow, I'm not familiar with the feedback/issue conventions of the python project and may have misunderstood the purpose of dialects, so feel free to correct me (:

  7. ericvsmith commented on Feb 4, 2025

    @ericvsmith
    ericvsmith
    on Feb 4, 2025
    Member
    More actions

    Could you provide sample code that shows what results you are getting, and explain how that differs from what expect to get?

  8. Nikilauz commented on Feb 5, 2025

    @Nikilauz
    Nikilauz
    on Feb 5, 2025
    Author
    More actions

    Sure:

    import csv
    
    s = '42,"hello","world",-1\n'    # e.g. via readline method from some csv file
    d = csv.Sniffer().sniff(s)
    d.quoting    # 0
    d.lineterminator    # '\r\n'
    
    x = list(csv.reader([s], d))    # [['42', 'hello', 'world', '-1']]
    
    f = open("test.csv", "w", newline = "")
    csv.writer(f, d).writerows(x)
    f.close()
    
    f = open("test.csv", "r+b")
    f.read()    # b'42,hello,world,-1\r\n'
    f.close()
    

    So instead of b'42,hello,world,-1\r\n' I would expect to get b'42,"hello","world",-1\n'.
    More precisely, I would expect d.quoting to be 2 (i.e. csv.QUOTE_NONNUMERIC) and d.lineterminator to be '\n'.

    Thanks for your time!

  9. aterrel commented on May 19, 2025

    @aterrel
    aterrel
    on May 19, 2025
    More actions

    What would you expect if there are a mix of string quoted and not quoted?

  10. Nikilauz commented on May 19, 2025

    @Nikilauz
    Nikilauz
    on May 19, 2025
    Author
    More actions

    Well then it's none of the possible values of Dialect.quoting and may be some fallback default quoting style I guess, i.e. 0 or so...

    Reacted by Andy R. Terrel
  11. aterrel commented on May 19, 2025

    @aterrel
    aterrel
    on May 19, 2025
    More actions

    Okay here is a test I'll build towards. Let me know if that correct.

    8a38c20#diff-dd5e5efc1566b1c1dc988769a04cc403d2416c8af5bf4785bec235d02a780813R1345

    Reacted by Nikilauz
  12. Nikilauz commented on May 20, 2025

    @Nikilauz
    Nikilauz
    on May 20, 2025
    Author
    More actions

    Looks good, depending on the assertEquals and getvalue methods there could be some issue with comparing \n and \r\n. With actual files that was a problem, thats why I used bytestrings...

  13. aterrel commented on May 21, 2025

    @aterrel
    aterrel
    on May 21, 2025
    More actions

    @Nikilauz I've added the PR for the feature. Would be great if you could pull down the dev version of Python and test it out a bit. Unfortunately there are some gotchas but this seems to be the beginning of a solution.

  14. serhiy-storchaka
    added
    3.16new features, bugs and security fixesnew features, bugs and security fixes
    on Jul 21, 2026
  15. serhiy-storchaka commented on Jul 21, 2026

    @serhiy-storchaka
    serhiy-storchaka
    on Jul 21, 2026
    Member
    More actions

    There is an older issue for detecting lineterminator:

    • csv.Sniffer does not detect lineterminator #75008
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesnew features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    • CSV issues 📊
      Status
      No status
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.