dongliao6491 2015-07-31 22:09
浏览 131

从已知的好的CSV文件获取EOF

I have been writing a program to parse a csv file using the encoding/csv package. For some reason I have begul getting sporadic EOF errors. It is very strange since I am using the same file to test with over and over. Sometimes it parses fine. Other times it exits with an EOF.

This is a simplified version of the code:

main.go

package main

import (
    "bytes"
    "encoding/csv"
    "fmt"
    "os"
    "path/filepath"
    "unicode/utf8"
)

func main() {
    LoadConfig()
    csvfile, err := os.Open(config.MonitorDir + "/test.csv")
    if err != nil {
        fmt.Println(err)
        os.Exit(2)
    }

    reader := csv.NewReader(csvfile)
    defer func() {
        csvfile.Close()
        MoveFile(csvfile)
    }()
    reader.Comma, _ = utf8.DecodeRuneInString(config.Delimiter)
    reader.LazyQuotes = true
    writeHeaders(reader)
}

func writeHeaders(reader *csv.Reader) {
  Headers, err := reader.Read() // This is where I see the EOF 
                                // (Headers = [] err = EOF)
  if err != nil {
      fmt.Printf("Error is: %s
", err.Error())
      os.Exit(2)
  }
  fmt.Println(Headers)
}

func MoveFile(file *os.File) {
    err := os.MkdirAll(Concat(config.MonitorDir, "/completed/"), os.ModeDir)
    if err != nil {
        fmt.Printf("Error is: %s
", err.Error())
        os.Exit(2)
    }
    err = os.Rename(file.Name(), Concat(config.MonitorDir, "/completed/", filepath.Base(file.Name())))

    if err != nil {
        return
    }
}

func Concat(str ...string) string {
    var buffer bytes.Buffer

    for i := 0; i < len(str); i++ {
        buffer.WriteString(str[i])
    }
    return buffer.String()
}

conf.go

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type Configuration struct {
    MonitorDir string
    LogPath    string
    Delimiter  string
}

var config Configuration

func LoadConfig() {
    file, _ := os.Open("conf.json")
    decoder := json.NewDecoder(file)
    config = Configuration{}
    err := decoder.Decode(&config)
    if err != nil {
        fmt.Printf("Error is: %s
", err.Error())
        os.Exit(2)
    }
}

test.csv

Id,LastModified
"108420","2015-04-25T11:34:35.437332096-06:00"
"132774","2015-04-25T11:34:35.437332096-06:00"
"254790","2015-04-25T11:34:35.437332096-06:00"
"359972","2015-04-25T11:34:35.437332096-06:00"
"363060","2015-04-25T11:34:35.437332096-06:00"

This has become a blocking issue as it might throw 8, 10 or even more EOF before finally processing the file. I presume I am doing something strange to cause it. But, I cannot for the life of me see it.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比