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

Implement mapping functions #275

Open
kubakl opened this issue Apr 16, 2024 · 0 comments
Open

Implement mapping functions #275

kubakl opened this issue Apr 16, 2024 · 0 comments

Comments

@kubakl
Copy link

kubakl commented Apr 16, 2024

I would like to have a clean way of sanitising my csvs before the unmarshaling happens. Take this csv as an example:

Name,Age,Height
John,10,1.80
Jack,20,N/A

In this case it will fail to decode due to N/A value. I would like to have a way to replace it with NaN or an empty string so it's omitted. The solution from csvutil package is a really easy and clean one.

	dec, err := csvutil.NewDecoder(r)
	if err != nil {
		log.Fatal(err)
	}

	dec.Map = func(field, column string, v any) string {
		if _, ok := v.(float64); ok && field == "n/a" {
			return "NaN"
		}
		return field
	}
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

1 participant