encoding/csv uses the io.Reader interface, so wouldn't you just need a 
CR->CR/LF filter that fulfills that interface?

Something like https://github.com/andybalholm/crlf should do the trick. 
Though it would be annoying to deal with when writing a generic program for 
handling arbitrary input, if you are dealing with an internal program and 
your own files and you know they are going to be generated that way...

f, err := os.Open("myfile.csv")
// handle error, defer close, etc
c := csv.NewReader(f)
// do stuff

would just become

f, err := os.Open("myfile.csv")
// handle error, defer close, etc
c := csv.NewReader(crlf.NewReader(f))
// do stuff

Howard

On Monday, July 17, 2017 at 10:07:43 PM UTC-5, Matt Harden wrote:
>
> I suspect that this has to do with the line-ending characters on a Mac. I 
> think Excel is writing the file with each line ending with a CR character. 
> The encoding/csv package expects RFC 4180 format (each line terminated with 
> CRLF), which is what Excel writes when you select "Windows Comma Separated".
>
> I don't know a super-easy way to make encoding/csv accept the first format.
>
> On Mon, Jul 17, 2017 at 4:42 PM Dat Huynh <audat...@gmail.com 
> <javascript:>> wrote:
>
>> Hi all,
>>
>> I have a problem with parsing a .csv file using the library 
>> "encoding/csv".
>>
>> I wonder if that is the problem of Microsoft Excel or the Go library.
>>
>> I am using Microsoft Excel version 14.2.2 on MacOS and go1.8.3 
>> darwin/amd64
>>
>> What did I do?
>>
>> Firstly I input the below values into an Excel sheet, and save as a .csv 
>> file.
>> value 11 value 12 
>> value 21 value 22 
>> value 31 value 32 
>>
>> If I choose "Comma Separated Values (.csv)" in the option "Format", type 
>> the file name "data.csv", and run my Go app, it returns:
>>
>> $ go run demo.go 
>> value 31 value 32]12
>>
>> If I choose "Window Comma Separated (.csv)" in the option "Format", type 
>> the file name "data.csv", and run my Go app, it works well.
>>
>> $ go run demo.go 
>> 0 [value 11 value 12]
>> 1 [value 21 value 22]
>> 2 [value 31 value 32]
>>
>> Could you please confirm if this is a bug of the library or MS Excel?
>>
>> Below is my code.
>>
>> package main
>>
>> import (
>> "encoding/csv"
>> "fmt"
>> "os"
>> )
>>
>> func main() {
>> file, _ := os.Open("data.csv")
>> defer file.Close()
>> csvReader := csv.NewReader(file)
>> records, _ := csvReader.ReadAll()
>> for index, record := range records {
>> fmt.Println(index, record)
>> }
>> }
>>
>> Thank you very much.
>>
>> Regards,
>> Dat Huynh.
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to golang-nuts...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to