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

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

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)

展示差异的示例代码。

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "strings"
  7. )
  8. func processFile(src string) {
  9. data, _ := ioutil.ReadFile(src)
  10. fmt.Println("--- 原始源码 ---")
  11. fmt.Println(string(data))
  12. fmt.Println(http.DetectContentType(data))
  13. fmt.Println("\n--- 修改后的源码 ---")
  14. for _, val := range strings.Split(string(data), "\n") {
  15. fmt.Println(strings.TrimSpace(val))
  16. }
  17. }
  18. func main() {
  19. processFile("./utf-16-english.txt")
  20. processFile("./utf-8-english.txt")
  21. }

文件-1

(utf-8-english.txt)

  1. Hello
  2. This is
  3. Sample
  4. Document

文件-2

(utf-16-english.txt)

  1. Hello
  2. This is
  3. Sample
  4. 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.

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "strings"
  7. )
  8. func processFile(src string) {
  9. data, _ := ioutil.ReadFile(src)
  10. fmt.Println("--- original source ---")
  11. fmt.Println(string(data))
  12. fmt.Println(http.DetectContentType(data))
  13. fmt.Println("\n--- modified source ---")
  14. for _, val := range strings.Split(string(data), "\n") {
  15. fmt.Println(strings.TrimSpace(val))
  16. }
  17. }
  18. func main() {
  19. processFile("./utf-16-english.txt")
  20. processFile("./utf-8-english.txt")
  21. }

File-1

(utf-8-english.txt)

  1. Hello
  2. This is
  3. Sample
  4. Document

File-2

(utf-16-english.txt)

  1. Hello
  2. This is
  3. Sample
  4. 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,之后你可以使用字符串库来处理输入。

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

  1. import "unicode/utf16"
  2. func processFile(src string, decode func(in []byte) string) {
  3. data, _ := ioutil.ReadFile(src)
  4. fmt.Println("--- 原始源码 ---")
  5. fmt.Println(decode(data))
  6. fmt.Println("\n--- 修改后的源码 ---")
  7. for _, val := range strings.Split(decode(data), "\n") {
  8. fmt.Println(strings.TrimSpace(val))
  9. }
  10. }
  11. func main() {
  12. processFile("./utf-16-english.txt", func(in []byte) string {
  13. return string(utf16.Decode(in))
  14. })
  15. processFile("./utf-8-english.txt", func(in []byte) string {
  16. return string(in)
  17. })
  18. }

这段代码会帮助你解码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:

  1. import "unicode/utf16"
  2. func processFile(src string, decode func(in[]byte) string) {
  3. data, _ := ioutil.ReadFile(src)
  4. fmt.Println("--- original source ---")
  5. fmt.Println(decode(data))
  6. fmt.Println("\n--- modified source ---")
  7. for _, val := range strings.Split(decode(data), "\n") {
  8. fmt.Println(strings.TrimSpace(val))
  9. }
  10. }
  11. func main() {
  12. processFile("./utf-16-english.txt",func(in []byte) string {
  13. return string(utf16.Decode(in)) })
  14. processFile("./utf-8-english.txt",func(in []byte) string {
  15. return string(in)})
  16. }

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:

确定