无法处理具有不同编码的文件中的字符串[不接受答案]

huangapple go评论82阅读模式
英文:

Unable to process strings in files with different encoding [Not accepting answers]

问题

我正在尝试处理一个特定文件中的字符串,该文件是用英文编写的。当文件的编码与"UTF-8"不同时,问题就出现了。但是,编码为"UTF-16-le"的文件的行为与预期不符。我的主要目标是操作读取文件中的字符串。例如,strings.TrimSpace()只适用于UTF-8文件。

我知道golang默认只支持UTF-8,任何替代方法都将很有帮助。

个人问题

>另外,我想指出,许多新的编程语言在处理字符串时不考虑编码。为什么Go只支持UTF-8?如果至少有一种替代方法可以将编码格式传递给读取器,那可能仍然有所帮助。

我尝试过的方法

  1. 我尝试使用utf-8和utf-16标准包。

代码

(main.go)

展示差异的示例代码。

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func processFile(src string) {
	data, _ := ioutil.ReadFile(src)

	fmt.Println("--- 原始源码 ---")
	fmt.Println(string(data))
	fmt.Println(http.DetectContentType(data))

	fmt.Println("\n--- 修改后的源码 ---")
	for _, val := range strings.Split(string(data), "\n") {
		fmt.Println(strings.TrimSpace(val))
	}
}

func main() {
	processFile("./utf-16-english.txt")
	processFile("./utf-8-english.txt")
}

文件-1

(utf-8-english.txt)

Hello
	This is
		Sample
			Document

文件-2

(utf-16-english.txt)

Hello
	This is
		Sample
			Document

编辑

  1. 看起来更好的处理字符串的唯一方法是将它们转换为UTF-8。请参考标记的答案。

  2. 根据评论,我已将程序的结果写入了相应的文件。特殊符号不再存在,但是使用字符串处理函数在UTF-8下工作正常。

英文:

I am trying to process the strings present in a particular file, The file is written in English. The problem arises when the encoding of the file differs from "UTF-8". But the file with encoding as "UTF-16-le" does not behave as expected. My main goal is manipulate the strings from the read file. For example the strings.TrimSpace() only works with the UTF-8 file,

I am aware that golang only supports UTF-8 by default, Any alternate approach would be helpful.

Personal Question

>Also I would like to point out, many new programming languages, do process the strings irrespective of the encoding, And why does Go only support UTF-8. If at least there would be an alternative way to pass the encoding format to the reader, that might still help.

What I tried

  1. I tried using utf-8 and utf-16 std packages

Code

(main.go)

sample code to show the difference.

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func processFile(src string) {
	data, _ := ioutil.ReadFile(src)

	fmt.Println("--- original source ---")
	fmt.Println(string(data))
	fmt.Println(http.DetectContentType(data))

	fmt.Println("\n--- modified source ---")
	for _, val := range strings.Split(string(data), "\n") {
		fmt.Println(strings.TrimSpace(val))
	}
}

func main() {
	processFile("./utf-16-english.txt")
	processFile("./utf-8-english.txt")
}

File-1

(utf-8-english.txt)

Hello
	This is
		Sample
			Document

File-2

(utf-16-english.txt)

Hello
	This is
		Sample
			Document

EDIT

  1. Seems that the only way to process strings in a better way is to convert them to UTF-8. Kindly refer the marked answer.

  2. As per comments I have written the result from the program to respective files. And the special symbols are not present, but the process with strings, works fine with UTF-8

答案1

得分: 0

你需要解码UTF-16编码的文件。解码将把输入转换为UTF-8,之后你可以使用字符串库来处理输入。

你可以使用类似下面的代码:

import "unicode/utf16"

func processFile(src string, decode func(in []byte) string) {
    data, _ := ioutil.ReadFile(src)

    fmt.Println("--- 原始源码 ---")
    fmt.Println(decode(data))

    fmt.Println("\n--- 修改后的源码 ---")
    for _, val := range strings.Split(decode(data), "\n") {
        fmt.Println(strings.TrimSpace(val))
    }
}

func main() {
    processFile("./utf-16-english.txt", func(in []byte) string {
        return string(utf16.Decode(in))
    })
    processFile("./utf-8-english.txt", func(in []byte) string {
        return string(in)
    })
}

这段代码会帮助你解码UTF-16编码的文件,并将其转换为UTF-8格式。然后,你可以使用字符串库来处理输入。

英文:

You have to decode the utf-16 encoded file. The decoding will convert the input to utf-8, after which you can use the string libraries to process the input.

You can use something like this:

import "unicode/utf16"

func processFile(src string, decode func(in[]byte) string) {
    data, _ := ioutil.ReadFile(src)

    fmt.Println("--- original source ---")
    fmt.Println(decode(data))

    fmt.Println("\n--- modified source ---")
    for _, val := range strings.Split(decode(data), "\n") {
        fmt.Println(strings.TrimSpace(val))
    }
}

func main() {
    processFile("./utf-16-english.txt",func(in []byte) string {
        return string(utf16.Decode(in)) })
    processFile("./utf-8-english.txt",func(in []byte) string {
         return string(in)})
}

huangapple
  • 本文由 发表于 2022年1月23日 12:26:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/70819119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定