How to remove special char in golang when reading file?

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

How to remove special char in golang when reading file?

问题

我有一个文件,内容如下:
每行代表一个网站

  1. 1 www.google.com$
  2. 2 www.apple.com$
  3. 3 www.facebook.com$

我在golang中这样读取它:

  1. type ListConf struct {
  2. File string
  3. Datas map[string]struct{}
  4. }
  5. func loadListConf(conf *ListConf, path string) {
  6. file, err := os.Open(path + "/" + conf.File)
  7. if err != nil {
  8. fmt.Println("加载配置文件" + conf.File + "出错:" + err.Error())
  9. return
  10. }
  11. defer file.Close()
  12. conf.Datas = make(map[string]struct{})
  13. buf := bufio.NewReader(file)
  14. end := false
  15. for !end {
  16. line, err := buf.ReadString('\n')
  17. if err != nil {
  18. if err != io.EOF {
  19. fmt.Println("加载配置文件" + conf.File + "出错:" + err.Error())
  20. return
  21. } else {
  22. end = true
  23. }
  24. }
  25. item := strings.Trim(line, "\n")
  26. if item == "" {
  27. continue
  28. }
  29. conf.Datas[item] = struct{}{}
  30. }
  31. }

但是当我在map中搜索"www.google.com"这样的键时,显示map中没有"www.google.com"。

  1. website := "www.google.com"
  2. if _, ok := conf.Datas[website]; ok {
  3. fmt.Printf("%s在map中。", website)
  4. } else {
  5. fmt.Printf("%s不在map中。", website)
  6. }

它打印出"www.google.com不在map中"。
我发现在map中的每个键的末尾有一个^M,我的问题是如何去除^M字符?

  1. www.google.com^M
  2. www.apple.com^M
  3. www.facebook.com^M
英文:

I have a file like this:
Each line represents a Website

  1. 1 www.google.com$
  2. 2 www.apple.com$
  3. 3 www.facebook.com$

I read it in golang like this:

  1. type ListConf struct {
  2. File string
  3. Datas map[string]struct{}
  4. }
  5. func loadListConf(conf *ListConf, path string) {
  6. file, err := os.Open(path + "/" + conf.File)
  7. if err != nil {
  8. fmt.Println("Load conf " + conf.File + " error: " + err.Error())
  9. return
  10. }
  11. defer file.Close()
  12. conf.Datas = make(map[string]struct{})
  13. buf := bufio.NewReader(file)
  14. end := false
  15. for !end {
  16. line, err := buf.ReadString('\n')
  17. if err != nil {
  18. if err != io.EOF {
  19. fmt.Println("Load conf " + conf.File + " error: " + err.Error())
  20. return
  21. } else {
  22. end = true
  23. }
  24. }
  25. item := strings.Trim(line, "\n")
  26. if item == "" {
  27. continue
  28. }
  29. conf.Datas[item] = struct{}{}
  30. }
  31. }

But when I search key such like "www.google.com" in the map, it shows that there is not a "www.google.com" in the map.

  1. website := "www.google.com"
  2. if _, ok := conf.Datas[website]; ok {
  3. fmt.Printf("%s is in the map.", website)
  4. } else {
  5. fmt.Printf("%s is not in the map.", website)
  6. }

It print "www.google.com is not in the map".
I found that a ^M in the end of each key in the map, my question is how can I remove the ^M character?

  1. www.google.com^M
  2. www.apple.com^M
  3. www.facebook.com^M

答案1

得分: 4

在Windows上,文本文件的默认行分隔符是由两个字符组成的序列:\r\n。你在字符串中看到的^M字符代表的是\r

bufio.Scanner可以以与平台无关的方式处理将输入拆分为行:

  1. scanner := bufio.NewScanner(file)
  2. for scanner.Scan() {
  3. fmt.Println(scanner.Text())
  4. }
  5. if err := scanner.Err(); err != nil {
  6. fmt.Fprintln(os.Stderr, "从文件中读取时出错:", err)
  7. }
英文:

The default line separator in text files on Windows is a sequence of two characters: \r\n. ^M character that you see in your strings is \r.

bufio.Scanner can take care of splitting the input into lines in a platform independent way:

  1. scanner := bufio.NewScanner(file)
  2. for scanner.Scan() {
  3. fmt.Println(scanner.Text())
  4. }
  5. if err := scanner.Err(); err != nil {
  6. fmt.Fprintln(os.Stderr, "error reading from the file:", err)
  7. }

答案2

得分: 2

不太优雅,但是你可以使用以下代码从字符串末尾删除\r

  1. line, err := buf.ReadString('\n')
  2. line = strings.TrimRight(line, "\r")

它会删除多个\r(^M),如果没有\r,则不会有任何操作。

英文:

Less elegant but...

You can strip the \r from the end of the string with:

  1. line, err := buf.ReadString('\n')
  2. line = strings.TrimRight(line, "\r")

It will remove multiple \r (^M) and is a no-op if there are none.

huangapple
  • 本文由 发表于 2016年2月1日 11:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/35122482.html
匿名

发表评论

匿名网友

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

确定