forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
importccl: Preserve '\r\n' during CSV import
See cockroachdb#25344. It appears this is being caused by the behaviour of Golang's encoding/csv library, which folds \r\n into \n when reading. This was fixed in golang/go#21201 but then reverted golang/go#22746. It appears based on that second issue that Go is unlikely to change that behavior. Check in the stdlib `encoding/csv` into `pkg/util` with golang/go#22746 reverted. Release note: `\r\n` characters in CSV files were silently converted into `\n`. This causes imported data to be different. This is now fixed.
- Loading branch information
Showing
10 changed files
with
1,257 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2015 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package csv_test | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
) | ||
|
||
func ExampleReader() { | ||
in := `first_name,last_name,username | ||
"Rob","Pike",rob | ||
Ken,Thompson,ken | ||
"Robert","Griesemer","gri" | ||
` | ||
r := csv.NewReader(strings.NewReader(in)) | ||
|
||
for { | ||
record, err := r.Read() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(record) | ||
} | ||
// Output: | ||
// [first_name last_name username] | ||
// [Rob Pike rob] | ||
// [Ken Thompson ken] | ||
// [Robert Griesemer gri] | ||
} | ||
|
||
// This example shows how csv.Reader can be configured to handle other | ||
// types of CSV files. | ||
func ExampleReader_options() { | ||
in := `first_name;last_name;username | ||
"Rob";"Pike";rob | ||
# lines beginning with a # character are ignored | ||
Ken;Thompson;ken | ||
"Robert";"Griesemer";"gri" | ||
` | ||
r := csv.NewReader(strings.NewReader(in)) | ||
r.Comma = ';' | ||
r.Comment = '#' | ||
|
||
records, err := r.ReadAll() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Print(records) | ||
// Output: | ||
// [[first_name last_name username] [Rob Pike rob] [Ken Thompson ken] [Robert Griesemer gri]] | ||
} | ||
|
||
func ExampleReader_ReadAll() { | ||
in := `first_name,last_name,username | ||
"Rob","Pike",rob | ||
Ken,Thompson,ken | ||
"Robert","Griesemer","gri" | ||
` | ||
r := csv.NewReader(strings.NewReader(in)) | ||
|
||
records, err := r.ReadAll() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Print(records) | ||
// Output: | ||
// [[first_name last_name username] [Rob Pike rob] [Ken Thompson ken] [Robert Griesemer gri]] | ||
} | ||
|
||
func ExampleWriter() { | ||
records := [][]string{ | ||
{"first_name", "last_name", "username"}, | ||
{"Rob", "Pike", "rob"}, | ||
{"Ken", "Thompson", "ken"}, | ||
{"Robert", "Griesemer", "gri"}, | ||
} | ||
|
||
w := csv.NewWriter(os.Stdout) | ||
|
||
for _, record := range records { | ||
if err := w.Write(record); err != nil { | ||
log.Fatalln("error writing record to csv:", err) | ||
} | ||
} | ||
|
||
// Write any buffered data to the underlying writer (standard output). | ||
w.Flush() | ||
|
||
if err := w.Error(); err != nil { | ||
log.Fatal(err) | ||
} | ||
// Output: | ||
// first_name,last_name,username | ||
// Rob,Pike,rob | ||
// Ken,Thompson,ken | ||
// Robert,Griesemer,gri | ||
} | ||
|
||
func ExampleWriter_WriteAll() { | ||
records := [][]string{ | ||
{"first_name", "last_name", "username"}, | ||
{"Rob", "Pike", "rob"}, | ||
{"Ken", "Thompson", "ken"}, | ||
{"Robert", "Griesemer", "gri"}, | ||
} | ||
|
||
w := csv.NewWriter(os.Stdout) | ||
w.WriteAll(records) // calls Flush internally | ||
|
||
if err := w.Error(); err != nil { | ||
log.Fatalln("error writing csv:", err) | ||
} | ||
// Output: | ||
// first_name,last_name,username | ||
// Rob,Pike,rob | ||
// Ken,Thompson,ken | ||
// Robert,Griesemer,gri | ||
} |
Oops, something went wrong.