Home > OS >  Read Non-UTF8 encoded file content and print out correctly
Read Non-UTF8 encoded file content and print out correctly

Time:12-26

I try to read non-UTF8 encoded file and print out the content. Like:

content, _ := os.ReadFile("example.csv")
fmt.Println(string(content))

Output:

����ҭ��dzԪ�� �Ӻ��Ҵ�˭�

Then, I tried to convert the content of the rune and decode it to utf8 like:

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

But the result was the same. How can I get right content? PS: I can not know the file encoding type, they can be several type like raditionalchinese.Big5 or japanese.ShiftJIS and content must not be file. It can be a string.

CodePudding user response:

Most probably you need packages from the golang.org/x/text/encoding hierarchy.

In particular, the golang.org/x/text/encoding/charmap allows creating a encoding.Decoder able to translate stream of bytes in a legacy non-UTF-8 encoding to a UTF-8-encoded stream of data—native to Go.

  • Related