Giter VIP home page Giter VIP logo

csv's People

Contributors

xinyi2016 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

csv's Issues

Propose a solution to customize quoting behaviour

Problem

I found 6 similar issues with parsing quoted fields in Go’s built-in CSV package encoding/csv. In this proposal, I show a solution that allows encoding/csv to handle all of those cases.

Firstly, CSV inputs in the above-mentioned issues do not conform to RFC 4180 format that encoding/csv follows. Some of them seem to be bad data.

For example, Issue #56329 expects "a"b"c",123, to be parsed as [a"b"c,123,]. This allows single quotes within quoted fields. If the quoted b was a comma that we try to escape, we see the problem:

“a”,”c”,123,

The input can be interpreted as

[a, c, 123]

or

[a”,”c, 123]

Solution

In this proposal, I want to introduce a function parameter that allows users to modify the malformed CSV input and utilize what has been built at the same time.

The modified parser looks like this:

csv/reader.go

Lines 295 to 323 in f0ad461

type Format func(o []byte) (n []byte)
func (r *Reader) readRecord(dst []string, fmts ...Format) ([]string, error) {
if r.Comma == r.Comment || !validDelim(r.Comma) || (r.Comment != 0 && !validDelim(r.Comment)) {
return nil, errInvalidDelim
}
// Read line (automatically skipping past empty lines and any comments).
var line []byte
var errRead error
for errRead == nil {
line, errRead = r.readLine()
if r.Comment != 0 && nextRune(line) == r.Comment {
line = nil
continue // Skip comment lines
}
if errRead == nil && len(line) == lengthNL(line) {
line = nil
continue // Skip empty lines
}
break
}
if errRead == io.EOF {
return nil, errRead
}
for _, applyFmt := range fmts {
line = applyFmt(line)
}

This design allows encoding/csv to handle malformed CSV inputs. It introduces a possibility for encoding/csv to support other CSV formats.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.