Home  >  Article  >  Backend Development  >  Determine how many bytes a CSV row is in Golang

Determine how many bytes a CSV row is in Golang

王林
王林forward
2024-02-05 23:33:081059browse

确定 Golang 中 CSV 行有多少字节

Question content

I have a test.csv and I will read it line by line and determine how many are in each line byte.

This should be lower than 37 bytes because I'm on Windows and the first two lines have \r\n adding a total of 4 bytes.

foo,bar,baz
100,200,300
400,500,600

I want to use csv.NewReader() to simply determine how many bytes are in each row. However, I know that when doing byte counting in the code below, csv.Reader does not count commas and \n in each row.

Should I add some math for the number of commas in each line and add 2 bytes for the last line as \r\n -1 since it doesn't have \r\n? This feels a bit hackneyed, so I'd rather see if there's a better solution to my byte count problem.

My code:

package main

import (
    "encoding/csv"
    "fmt"
    "io"
    "log"
    "os"
)

func main() {
    file, err := os.Open("test.csv")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    fileInfo, err := file.Stat()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("file total bytes is %d\n", fileInfo.Size())

    // init reader
    reader := csv.NewReader(file)

    // extract the header
    headers, err := reader.Read()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("headers are: %+v\n", headers)

    byteCounter := 0
    for {
        // if we reached end of file, stop
        if err == io.EOF {
            break
        }

        // read a record
        record, err := reader.Read()
        if err != nil {
            log.Fatal(err)
        }
        // loop through each record and count how many bytes
        for _, item := range record {
            byteCounter += len(item)
            fmt.Printf("record is %d bytes\n", len(item))
        }
        fmt.Println("total bytes so far is: ", byteCounter)

    }
}

Correct answer


Use Reader.InputOffset to get the current position in the file: BD44F3686574B2610B9F6F11A1F9CFEF

https://www.php.cn/link/e2c2ff9c7fb57db574e461fbe467d84e

To get the number of bytes per row, subtract the previous offset.

prevOffset := reader.InputOffset()

for {
    ...
    // read a record
    _, err := reader.Read()
    ...
    fmt.Println("line length is: ", reader.InputOffset()-prevOffset)
    prevOffset = reader.InputOffset()

}

https://www.php.cn/link/501e3f8a108d7ab9335ceecd363d113d

The above is the detailed content of Determine how many bytes a CSV row is in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete