gpt4 book ai didi

go - 在栏位名称前撷取csv空间

转载 作者:行者123 更新时间:2023-12-01 21:09:45 24 4
gpt4 key购买 nike

我有一个奇怪的问题,我有这个CSV

NAME,AGE,CITY
Bob,12,London
James,18,London
Joe,55,Manchester
Simon,21,Manchester
和下面的代码。但是,当我提取文件时,它在第一个字段名称之前带有一个空格(如下所示)
enter image description here
下面我尝试了字符串修剪和字符串替换,但是都没有用。有人有什么想法吗?
func main() {
var files []string

root := "C:\\Users\\kiera\\OneDrive\\Documents\\golang\\df\\filewalk\\csvs"

err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
files = append(files, path)
return nil
})
if err != nil {
panic(err)
}
for _, file := range files {
//open the file
openfile, _ := ioutil.ReadFile(file)

//cast byte slice as string
contents := string(openfile)

//get rid of whitespace
contents = strings.TrimSpace(contents)
contents = strings.ReplaceAll(contents, " NAME", "NAME")
fmt.Println(contents)
}
}

最佳答案

如果您将文件另存为utf-8,则使用no​​tepad++,notepad,netbeans等
它在文件的开头添加了隐藏字符(BOM)。
只需更改您的IDE或禁用BOM。
例如在notepad++中,您可以按照以下指示操作https://www.ibm.com/support/pages/how-remove-bom-any-textxml-file
通常,此问题发生在Windows中。

关于go - 在栏位名称前撷取csv空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62733498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com