douhuan1497 2013-07-15 21:26
浏览 57
已采纳

恐慌:行尾有多余的定界符

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.

  • 写回答

2条回答 默认 最新

  • dswqw66280 2013-07-15 22:24
    关注

    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 :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序