如何在 csv 文件中空 space 的地方将空字符串值添加到数据库中
How on the place of empty space in the csv file the empty string value is added in database
我的 csv 文件有这样的用户数据:
firstname|lastname|Email| other fields are empty
| | |
| | |
| | |
| | |
我使用的代码是:
package main
import (
"encoding/csv"
"gopkg.in/mgo.v2"
"io"
"log"
"os"
)
type Mongo struct {
// Id int `json:"_id" bson:"_id"`
FirstName string `json:"first_name,omitempty" bson:"first_name,omitempty"`
LastName string `json:"last_name,omitempty" bson:"last_name,omitempty"`
Email string `json:"email,omitempty" bson:"email,omitempty"`
PhoneNumber string `json:"phone_number,omitempty" bson:"phone_number,omitempty"`
Gender string `json:"gender,omitempty" bson:"gender,omitempty"`
Address string `json:"address,omitempty" bson:"address,omitempty"`
Apartment string `json:"apartment,omitempty" bson:"apartment,omitempty"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
}
func main() {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("Import_Users").C("users")
file, err := os.Open("customers.csv")
if err != nil {
panic(err)
}
defer file.Close()
reader := csv.NewReader(file)
for {
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
err = c.Insert(&Mongo{record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7]})
if err != nil {
panic(err)
}
log.Printf("%#v", record)
}
}
在 运行:
时出现错误
Error:-
panic: runtime error: index out of range
goroutine 1 [running]:
main.main()
/home/iron/go/src/go-training/How_to_load_csv_file/main.go:53 +0x830
exit status 2
如何解决这个错误?
确保您有 record[0]
到 record[7]
的切片元素。添加空填充元素。例如,
reader := csv.NewReader(file)
padding := make([]string, 8) // 0 thru 7
for {
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
record = append(record, padding...)
err = c.Insert(&Mongo{record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7]})
if err != nil {
panic(err)
}
log.Printf("%#v", record)
}
我的 csv 文件有这样的用户数据:
firstname|lastname|Email| other fields are empty
| | |
| | |
| | |
| | |
我使用的代码是:
package main
import (
"encoding/csv"
"gopkg.in/mgo.v2"
"io"
"log"
"os"
)
type Mongo struct {
// Id int `json:"_id" bson:"_id"`
FirstName string `json:"first_name,omitempty" bson:"first_name,omitempty"`
LastName string `json:"last_name,omitempty" bson:"last_name,omitempty"`
Email string `json:"email,omitempty" bson:"email,omitempty"`
PhoneNumber string `json:"phone_number,omitempty" bson:"phone_number,omitempty"`
Gender string `json:"gender,omitempty" bson:"gender,omitempty"`
Address string `json:"address,omitempty" bson:"address,omitempty"`
Apartment string `json:"apartment,omitempty" bson:"apartment,omitempty"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
}
func main() {
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
c := session.DB("Import_Users").C("users")
file, err := os.Open("customers.csv")
if err != nil {
panic(err)
}
defer file.Close()
reader := csv.NewReader(file)
for {
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
err = c.Insert(&Mongo{record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7]})
if err != nil {
panic(err)
}
log.Printf("%#v", record)
}
}
在 运行:
时出现错误Error:- panic: runtime error: index out of range
goroutine 1 [running]: main.main() /home/iron/go/src/go-training/How_to_load_csv_file/main.go:53 +0x830 exit status 2
如何解决这个错误?
确保您有 record[0]
到 record[7]
的切片元素。添加空填充元素。例如,
reader := csv.NewReader(file)
padding := make([]string, 8) // 0 thru 7
for {
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
record = append(record, padding...)
err = c.Insert(&Mongo{record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7]})
if err != nil {
panic(err)
}
log.Printf("%#v", record)
}