dongshou1856 2019-04-11 21:58
浏览 177
已采纳

在Golang中记录并查找CSV文件位置

I need to read a CSV file and record the locations of lines with certain values into an array, then later go back and retrieve those lines in no particular order and with good performance, so random access.

My program uses csv.NewReader(file), but I see no way to get or set the file offset that it uses. I tried file.Seek(0,io.SeekCurrent) to return the file position, but it doesn't change between calls to reader.Read(). I also tried fmt.Println("+v +v ",reader,file) to see if anything stores the reader's file position, but I don't see it. I also don't know the best way to use the file position if I do find it.

Here's what I need to do:

file,_ = os.Open("stuff.csv")
reader = csv.NewReader(file)

//read file and record locations
for {
    line,_ = reader.Read()
    if wantToRememberLocation(line) {
         locations = append(locations, getLocation()) //need this function
    }
}

//then revisit certain lines
for {
    reader.GoToLine(locations[random])  //need this function
    line,_ = reader.Read()
    doStuff(line)
}

Is there even a way to do this with the csv library, or will I have to write my own using more primitive file io functions?

  • 写回答

2条回答 默认 最新

  • doucan9079 2019-04-12 22:24
    关注

    Here's a solution using TeeReader. This example just saves all the positions and goes back and rereads some of them.

    //set up some vars and readers to record position and length of each line
    type Record struct {
        Pos int64
        Len int 
    }
    records := make([]Record,1)
    var buf bytes.Buffer
    var pos int64
    file,_ := Open("stuff.csv")
    tr := io.TeeReader(file, &buf)
    cr := csv.NewReader(tr)
    
    //read first row and get things started
    data,_ := cr.Read()
    dostuff(data)
    //length of current row determines position of next
    lineBytes,_ := buf.ReadBytes('
    ')
    length := len(lineBytes)
    pos += int64(length)
    records[0].Len = length
    records = append(records, Record{ Pos: pos })
    
    for i:=1;;i++ {
        //read csv data
        data,err = c.Read()
        if err != nil {break}
        dostuff(data)
        //record length and position
        lineBytes,_ = buf.ReadBytes('
    ')
        lenth = len(lineBytes)
        pos += int64(length)
        records[i].Len = length
        records = append(records, Record{ Pos: pos })
    }
    
    //prepare individual line reader
    line := make([]byte,1000)
    lineReader := bytes.NewReader(line)
    
    //read random lines from file
    for {
        i := someLineNumber()
        //use original file reader to fill byte slice with line
        file.ReadAt(line[:records[i].Len], records[i].Pos)
        //need new lineParser to start at beginning every time
        lineReader.Seek(0,0)
        lineParser := csv.NewReader(lineReader)
        data,_ = lineParser.Read()
        doStuff(data)
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵