artonge / go-csv-tag Goto Github PK
View Code? Open in Web Editor NEWRead csv file from go using tags
License: GNU General Public License v3.0
Read csv file from go using tags
License: GNU General Public License v3.0
The library fails to parse CSV files that contain lines with trailing spaces. I came across this issue when trying to read a GTFS file using the go-gtfs library.
E.g.:
header1, header2, header3
line1, 1, 1.2
line2, 2, 2.3
line3, 3, 3.4
Error produced:
error mapping csv from path csv_files/trailingSpaces.csv:
==> error mapping the content to the destination
==> line: [line1 1 1.2 ] to slice: {line1 1 0}:
==> error parsing float '1.2 ':
==> strconv.ParseFloat: parsing "1.2 ": invalid syntax
Hi I was trying to use this package to decode a struct with int64 fields and It wasn't working. There is a simple fix for this in load.go:
func storeValue(rawValue string, valRv reflect.Value) error {
switch valRv.Kind() {
case reflect.String:
valRv.SetString(rawValue)
case reflect.Int64: // Add this.
fallthrough
case reflect.Int:
value, err := strconv.ParseInt(rawValue, 10, 64)
if err != nil && rawValue != "" {
return fmt.Errorf("error parsing int '%v':\n ==> %v", rawValue, err)
}
It would be nice if we could specify a custom tag key. An example use case is where we might want to make use of existing tags such as json
. A working example here
When Dump
ing structs containing floats, the default representation is used. For high exponents, this is scientific notation, e.g. 1e-06
for 0.000001
.
This is incompatible with some specifications that use csv. Currently, there's no way of specifying which representation go-csv-tag
should use. Similar situations may arrise for other types, like if an integer should be duped as a hexadeximal value.
First use the example DumpToFile to generate a csv test file, then use LoadFromPath to load this file, panic happened
generated csv file :
name, ID, number
'name1', 1, 1.2
panic: runtime error: index out of range [1] with length 1
goroutine 1 [running]:
github.com/artonge/go-csv-tag.mapToDestination(0xc0000c7f48, 0x3, 0x3, 0xc0000b4
330, 0x2, 0x2, 0x4bbf00, 0xc000096560, 0x3, 0xc0000b4330)
E:/Go/gopath/src/github.com/artonge/go-csv-tag/load.go:182 +0xe9f
github.com/artonge/go-csv-tag.LoadFromReader(0x503ac0, 0xc0000ca028, 0x4bbf00, 0
xc000096560, 0xc0000c7f28, 0x1, 0x1, 0xc00008c8f0, 0xc0000a00c0)
E:/Go/gopath/src/github.com/artonge/go-csv-tag/load.go:48 +0x17e
github.com/artonge/go-csv-tag.LoadFromPath(0x4e52c4, 0x11, 0x4bbf00, 0xc00009656
0, 0xc0000c7f28, 0x1, 0x1, 0x0, 0x0)
E:/Go/gopath/src/github.com/artonge/go-csv-tag/load.go:78 +0x10b
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.