How to solve the garbled problem of golang CSV files

PHPz
Release: 2023-04-03 13:46:26
Original
2041 people have browsed it

With the widespread use of CSV files in data exchange, more and more developers are beginning to pay attention to the reading and writing of CSV files. As an excellent language, Golang naturally provides its own CSV library to facilitate developers to read and write CSV files. However, when developers use golang to read CSV files, they sometimes encounter garbled characters. This article will discuss how to solve the problem of garbled CSV files in golang.

  1. CSV file encoding

CSV file is a plain text file, and its encoding method can be many, such as UTF-8, GBK, GB2312, etc. In golang, reading CSV files uses UTF-8 encoding by default, so if the CSV file uses other encoding formats, garbled characters may occur.

  1. Solution

For the problem of garbled CSV files, there are several solutions:

2.1 Specify the file encoding

Yes Avoid garbled characters by setting the encoding of CSV files. Taking GBK encoding as an example, the code is as follows:

package main

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

func main() {
    f, err := ioutil.ReadFile("<filename>")
    if err != nil {
        log.Fatal(err)
    }

    r := csv.NewReader(transform.NewReader(bytes.NewReader(f), simplifiedchinese.GBK.NewDecoder()))
    records, err := r.ReadAll()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(records)
}
Copy after login

You can see that the ReadFile function is used in the code to read the CSV file, and is specified by setting NewDecoder The file encoding is GBK.

2.2 Using third-party libraries

In addition to the built-in CSV library, there are many excellent third-party CSV libraries available in golang. When reading CSV files, you can use these libraries to avoid garbled characters. If you use the go-csv library, you can set the encoding method when reading CSV files:

package main

import (
    "fmt"
    "github.com/gocarina/gocsv"
    "os"
)

type Record struct {
    Name string `csv:"name"`
    Age  int    `csv:"age"`
}

func main() {
    f, err := os.Open("<filename>")
    if err != nil {
        fmt.Println(err)
        return
    }

    var records []Record
    if err := gocsv.Unmarshal(f, &records); err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println(records)
}
Copy after login

As you can see, the gocsv library is used in the code to read CSV file, and the file encoding is set to GBK when parsing.

  1. Summary

CSV file garbled problem is not uncommon in golang, but by specifying file encoding and using third-party libraries, we can easily avoid it this problem. If you also encounter the problem of garbled CSV files, you can try the above two solutions.

The above is the detailed content of How to solve the garbled problem of golang CSV files. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!