doumie6223 2017-05-16 18:55
浏览 185
已采纳

Golang:在处理CSV时,是否重新格式化单行?

My golang CSV processing routine copies almost exactly from the Package CSV example:

func processCSV(path string){

    file:= utils.OpenFile(path)
    reader:= csv.NewReader(file)
    reader.LazyQuotes = true

    cs:= []*Collision{} //defined elsewhere

    for {

        line, err := reader.Read()

        //Kill processing if we're at EOF
        if err == io.EOF {
            break
        }

        c := get(line) //defined elsewhere
        cs= append(cs, c)
    }

    //Do other stuff...
}

The code works great until it encounters a malformed (?) line of CSV, which generally looks something like this:

item1,item2,"item3,"has odd quoting"","item4",item5

The csvReader.LazyQuotes = true option doesn't seem to offer enough tolerance to read this line as I need it.

My question is this: can I ask the csv reader for the original line so that I can "massage" it to pull out what I need? The files I'm working with are moderately large (~150mb) and I'm not sure I want to re-do them, especially as only a few lines per file have such problems.

Thanks for any tips!

  • 写回答

3条回答 默认 最新

  • douliexing2195 2017-05-16 19:36
    关注

    As far as I can tell encoding/csv doesn't provide any such functionality, so you can either look for some 3rd party csv package that does that, or you can implement a solution yourself.

    If you want to go the DIY route I can offer you a tip, whether it's a good tip that you should implement is up to you.

    You could implement an io.Reader that wraps your file and tracks the last line read, then every time you encouter an error because of malformed csv you can use your reader to reread that line, massage it, add it to the results, and have the loop continue as if nothing happened.

    Here's an example of how your processCSV would change:

    func processCSV(path string){
    
        file := utils.OpenFile(path)
        myreader := NewMyReader(file)
        reader := csv.NewReader(myreader)
        reader.LazyQuotes = true
    
        cs:= []*Collision{} //defined elsewhere
    
        for {
    
            line, err := reader.Read()
    
            //Kill processing if we're at EOF
            if err == io.EOF {
                break
            }
    
            // malformed csv
            if err != nil {
                // Just reread the last line and on the next iteration of
                // the loop myreader.Read should continue returning bytes 
                // that come after this malformed line to the csv.Reader.
                l, err := myreader.CurrentLine()
                if err != nil {
                    panic(err)
                }
    
                // massage the malformed csv line
                line = fixcsv(l) 
            }
    
            c := get(line) //defined elsewhere
            cs= append(cs, c)
        }
    
        //Do other stuff...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?