Go恐慌:行尾有额外的分隔符

huangapple go评论91阅读模式
英文:

Go panic: extra delimiter at end of line

问题

我正在使用Go读取MaxMind GeoIP Lite城市位置CSV文件:

csvFile, err := os.Open("/path/GeoLiteCity_20130702/GeoLiteCity-Location.csv")
defer csvFile.Close()

if err != nil {
	panic(err)
}

csvf := csv.NewReader(csvFile)
csvf.Read()		// 跳过标题行


for {
	fields, err := csvf.Read()

	if err == io.EOF {
		break
	} else if err != nil {
		panic(err)
	}

	// 目前什么都不做
}

我得到的错误是:

panic: 第2行,第22列:行尾有多余的分隔符

goroutine 1 [running]: main.main()
/path/myprogram.go:239
+0x108f

goroutine 2 [runnable]: exit status 2

文件非常长,但是以以下行开始:

locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
1,O1,,,,0.0000,0.0000,,
2,AP,,,,35.0000,105.0000,,
3,EU,,,,47.0000,8.0000,,
4,AD,,,,42.5000,1.5000,,
5,AE,,,,24.0000,54.0000,,
6,AF,,,,33.0000,65.0000,,
7,AG,,,,17.0500,-61.8000,,
8,AI,,,,18.2500,-63.1667,,
9,AL,,,,41.0000,20.0000,,

看起来格式是正确的。每行有9个字段。

第239行是我调用panic的行,panic(err)。如你所见,它在CSV文件的第2行失败,这发生在循环的第一次迭代中(第1行在循环之前读取,用于跳过标题行)。第2行的第22列是倒数第二个逗号。

我是否漏掉了什么?我没有看到任何尾随逗号...(澄清:每行末尾的逗号必须存在以表示空字段值,所以它们不是尾随的,即额外的。)

更新: Gophers已解决此问题,并在Go 1.1.2中修复

英文:

I'm reading the MaxMind GeoIP Lite City locations CSV file using Go:

csvFile, err := os.Open("/path/GeoLiteCity_20130702/GeoLiteCity-Location.csv")
defer csvFile.Close()

if err != nil {
	panic(err)
}

csvf := csv.NewReader(csvFile)
csvf.Read()		// skip header row


for {
	fields, err := csvf.Read()

	if err == io.EOF {
		break
	} else if err != nil {
		panic(err)
	}

	// does nothing yet
}

The error I'm getting is:

> panic: line 2, column 22: extra delimiter at end of line
>
> goroutine 1 [running]: main.main()
> /path/myprogram.go:239
> +0x108f
>
> goroutine 2 [runnable]: exit status 2

The file is quite long, but starts with these lines:

locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
1,O1,,,,0.0000,0.0000,,
2,AP,,,,35.0000,105.0000,,
3,EU,,,,47.0000,8.0000,,
4,AD,,,,42.5000,1.5000,,
5,AE,,,,24.0000,54.0000,,
6,AF,,,,33.0000,65.0000,,
7,AG,,,,17.0500,-61.8000,,
8,AI,,,,18.2500,-63.1667,,
9,AL,,,,41.0000,20.0000,,

It appears to be properly formatted. Each row has 9 fields.

Line 239 is my line invoking the panic, panic(err). As you can see, it's failing on line 2 of the CSV file, which happens in the first iteration of the loop (line 1 is read before the loop, to skip the header row). Column 22 of line 2 is the second-to-last comma.

Am I missing something here? I don't see any trailing comma... (clarification: the commas at the end of each line must be there to indicate empty field values, so they're not trailing, as in, extra.)

UPDATE: The gophers have resolved this issue and the fix ships with Go 1.1.2.

答案1

得分: 5

每行甚至有两个尾逗号。

尝试设置 csv.Reader.TrailingComma = true

查看源代码或至少包文档通常会有很大帮助 Go恐慌:行尾有额外的分隔符

英文:

There are even two trailing commas on each line.

Try setting csv.Reader.TrailingComma = true.

It really often helps taking a look at the source or at least the package documentation Go恐慌:行尾有额外的分隔符

答案2

得分: 1

这是一个完整的示例。关键是 csvf.TrailingComma = true

package main

import (
	"bytes"
	"encoding/csv"
	"fmt"
	"io"
)

var csvData = `locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
1,O1,,,,0.0000,0.0000,,
2,AP,,,,35.0000,105.0000,,
3,EU,,,,47.0000,8.0000,,
4,AD,,,,42.5000,1.5000,,
5,AE,,,,24.0000,54.0000,,
6,AF,,,,33.0000,65.0000,,
7,AG,,,,17.0500,-61.8000,,
8,AI,,,,18.2500,-63.1667,,
9,AL,,,,41.0000,20.0000,,
`

func main() {
	csvFile := bytes.NewBufferString(csvData)
	csvf := csv.NewReader(csvFile)
	csvf.TrailingComma = true
	csvf.Read() // 跳过标题行

	for {
		fields, err := csvf.Read()

		if err == io.EOF {
			break
		} else if err != nil {
			panic(err)
		}

		// 目前什么也不做
		fmt.Println(fields)
	}
}
英文:

Here is a complete example for you. The key is csvf.TrailingComma = true.

package main

import (
	"bytes"
	"encoding/csv"
	"fmt"
	"io"
)

var csvData = `locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode
1,O1,,,,0.0000,0.0000,,
2,AP,,,,35.0000,105.0000,,
3,EU,,,,47.0000,8.0000,,
4,AD,,,,42.5000,1.5000,,
5,AE,,,,24.0000,54.0000,,
6,AF,,,,33.0000,65.0000,,
7,AG,,,,17.0500,-61.8000,,
8,AI,,,,18.2500,-63.1667,,
9,AL,,,,41.0000,20.0000,,
`

func main() {
	csvFile := bytes.NewBufferString(csvData)
	csvf := csv.NewReader(csvFile)
	csvf.TrailingComma = true
	csvf.Read() // skip header row

	for {
		fields, err := csvf.Read()

		if err == io.EOF {
			break
		} else if err != nil {
			panic(err)
		}

		// does nothing yet
		fmt.Println(fields)
	}
}

huangapple
  • 本文由 发表于 2013年7月16日 05:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/17664246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定