Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First column cannot be populated #76

Closed
zhezack opened this issue Sep 28, 2017 · 6 comments
Closed

First column cannot be populated #76

zhezack opened this issue Sep 28, 2017 · 6 comments

Comments

@zhezack
Copy link

zhezack commented Sep 28, 2017

csv data:

number,column_a,column_b
1,a,b
2,c,d
3,e,f`

my struct:

type Whatever struct{
     JustNumber   int     `csv:"number"`
     ColumnA      string  `csv:"column_a"`
     ColumnB      string  `csv:"column_b"`
}

the result will be:

{0   a   b}
{0   c   d}
{0   e   f}

but if csv data like this:

,number,column_a,column_b
,1,a,b
,2,c,d
,3,e,f`

the result will be:

{1   a   b}
{2   c   d}
{3   e   f}

why first column cannot be populate to struct?

@pikanezi
Copy link
Member

Hello,

I just tried with this sample:

b := bytes.NewBufferString(`number,column_a,column_b
1,a,b
2,c,d
3,e,f`)
	var samples []Whatever
	if err := Unmarshal(b, &samples); err != nil {
		t.Fatal(err)
	}
	log.Println(samples)

and it prints [{1 a b} {2 c d} {3 e f}].

What method did you used?

@zhezack
Copy link
Author

zhezack commented Sep 29, 2017

i read from file. If i read from string work correctly

os.OpenFile("sample.csv", os.O_RDWR, os.ModePerm)

@zhezack
Copy link
Author

zhezack commented Sep 29, 2017

solved my csv file was wrong i dont know why.
thanks

@zhezack zhezack closed this as completed Sep 29, 2017
@SirRamEsq
Copy link

SirRamEsq commented Oct 2, 2018

I had a similar issue. For those who may be struggling, here was my situation/solution

I had exported a .csv file from MS Excel. I exported it out in UTF-8 format (or, so I thought). The file looked fine in Notepad++ at first glance, but gocsv would not find the first column.
Upon opening the .csv in Notepad++ and looking at the Encoding, I saw that it was not UTF-8, but UTF-8-BOM
Changing the Encoding to UTF-8 and saving the file allowed me to get the first column of .csv using gocsv

@bfitzsimmons
Copy link

@SirRamEsq I had this exact same issue. Thank you for sharing your solution!

@dewey
Copy link

dewey commented Aug 20, 2019

Yep, it's the invisible BOM characters at the beginning of the file. I wrote this up in more detail here because I had the same problem with this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants