我有一個奇怪的問題 我有這個 CSVNAME,AGE,CITYBob,12,LondonJames,18,LondonJoe,55,ManchesterSimon,21,Manchester以及下面的代碼。但是當我攝取文件時,它在第一個字段名稱之前帶有一個空格(如下所示)下面我嘗試了字符串修剪和字符串替換,但都沒有奏效。有沒有人有任何想法?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) }}
1 回答

忽然笑
TA貢獻1806條經驗 獲得超5個贊
如果您使用 notepad++、notepad、netbeans 等,如果您將文件保存為 utf-8,則會在文件開頭添加隱藏字符 (BOM)。只需更改您的 IDE 或禁用 BOM。例如在記事本++中,您可以按照以下說明操作https://www.ibm.com/support/pages/how-remove-bom-any-textxml-file
大多數情況下,這個問題發生在 Windows 中。
- 1 回答
- 0 關注
- 142 瀏覽
添加回答
舉報
0/150
提交
取消