Read non-UTF8 encoded file contents and print them correctly

王林
Release: 2024-02-06 08:27:09
forward
429 people have browsed it

Read non-UTF8 encoded file contents and print them correctly

Question content

I try to read a non-utf8 encoded file and print out the content. like:

content, _ := os.readfile("example.csv")
fmt.println(string(content))
Copy after login

Output:

����������������������������

I then try to convert the contents of the rune and decode it to utf8 like this:

br := make([]rune, 0)
for len(content) > 0 {
    r, size := utf8.DecodeRune(content)
    br = append(br, r)
    content = content[size:]
}
fmt.Println(string(br))
Copy after login

But the result is the same. How can I get the correct content? ps: I don't know the file encoding type, they can be several types such as traditionalchinese.big5 or japanese.shiftjis, and the content cannot be a file. It can be a string.


Correct answer


Most likely you need the package text/encoding from golang.org/x/ hierarchy structure.

In particular golang.org/x/text/encoding /charmap allows the creation of encodings. Decoder can convert byte streams in traditional non-UTF-8 encoding into Go's native UTF-8 encoded data stream.

The above is the detailed content of Read non-UTF8 encoded file contents and print them correctly. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
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