How to remove special char in golang when reading file?

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

How to remove special char in golang when reading file?

问题

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

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

我在golang中这样读取它:

type ListConf struct {
    File  string
    Datas map[string]struct{}
}

func loadListConf(conf *ListConf, path string) {
    file, err := os.Open(path + "/" + conf.File)
    if err != nil {
	    fmt.Println("加载配置文件" + conf.File + "出错:" + err.Error())
	    return
    }
    defer file.Close()
    conf.Datas = make(map[string]struct{})
    buf := bufio.NewReader(file)
    end := false
    for !end {
	    line, err := buf.ReadString('\n')
	    if err != nil {
		    if err != io.EOF {
			    fmt.Println("加载配置文件" + conf.File + "出错:" + err.Error())
			    return
		    } else {
			    end = true
		    }
	    }
	    item := strings.Trim(line, "\n")
	    if item == "" {
		    continue
	    }
	    conf.Datas[item] = struct{}{}
    }
}

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

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

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

www.google.com^M
www.apple.com^M
www.facebook.com^M
英文:

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

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

I read it in golang like this:

type ListConf struct {
    File  string
    Datas map[string]struct{}
}

func loadListConf(conf *ListConf, path string) {
    file, err := os.Open(path + "/" + conf.File)
    if err != nil {
	    fmt.Println("Load conf " + conf.File + " error: " + err.Error())
	    return
    }
    defer file.Close()
    conf.Datas = make(map[string]struct{})
    buf := bufio.NewReader(file)
    end := false
    for !end {
	    line, err := buf.ReadString('\n')
	    if err != nil {
		    if err != io.EOF {
			    fmt.Println("Load conf " + conf.File + " error: " + err.Error())
			    return
		    } else {
			    end = true
		    }
	    }
	    item := strings.Trim(line, "\n")
	    if item == "" {
		    continue
	    }
	    conf.Datas[item] = struct{}{}
    }
}

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.

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

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?

www.google.com^M
www.apple.com^M
www.facebook.com^M

答案1

得分: 4

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

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

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

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:

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

答案2

得分: 2

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

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

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

英文:

Less elegant but...

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

        line, err := buf.ReadString('\n')
        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:

确定