- 使用 deepseek实现 go语言,读取文本文件的功能,要求支持 ascii,utf-8 等多种格式自适应
- 我要用 chatgpt,也问过,但是比 deepseek 还是差一个级别,具体如下:
package mainimport ("bufio""bytes""fmt""golang.org/x/net/html/charset""golang.org/x/text/encoding/unicode""golang.org/x/text/transform""io""os""strings"
)
func detectBOM(b []byte) (enc string, bomLen int) {if len(b) >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF {return "UTF-8 BOM", 3}if len(b) >= 2 {if b[0] == 0xFF && b[1] == 0xFE {return "UTF-16LE", 2}if b[0] == 0xFE && b[1] == 0xFF {return "UTF-16BE", 2}}if len(b) >= 4 {if b[0] == 0xFF && b[1] == 0xFE && b[2] == 0x00 && b[3] == 0x00 {return "UTF-32LE", 4}if b[0] == 0x00 && b[1] == 0x00 && b[2] == 0xFE && b[3] == 0xFF {return "UTF-32BE", 4}}return "", 0
}
func ReadTextFile(filename string) ([]string, error) {file, err := os.Open(filename)if err != nil {return nil, err}defer file.Close()bomBuf := make([]byte, 4)n, err := file.Read(bomBuf)if err != nil && err != io.EOF {return nil, err}bomBuf = bomBuf[:n]enc, bomLen := detectBOM(bomBuf)var source io.Readervar decoder transform.Transformerif enc != "" {remaining := bomBuf[bomLen:]source = io.MultiReader(bytes.NewReader(remaining), file)switch enc {case "UTF-8 BOM":decoder = unicode.UTF8.NewDecoder()case "UTF-16LE":decoder = unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()case "UTF-16BE":decoder = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder()default:return nil, fmt.Errorf("unsupported encoding: %s", enc)}} else {source = io.MultiReader(bytes.NewReader(bomBuf), file)br := bufio.NewReader(source)detectBuf, err := br.Peek(1024)if err != nil && err != io.EOF {return nil, err}e, _, _ := charset.DetermineEncoding(detectBuf, "")if e == nil {e = unicode.UTF8 }decoder = e.NewDecoder()source = br}utf8Reader := transform.NewReader(source, decoder)scanner := bufio.NewScanner(utf8Reader)var lines []stringfor scanner.Scan() {lines = append(lines, scanner.Text())}if err := scanner.Err(); err != nil {return nil, err}return lines, nil
}
func Filter(lists []string) []string {r := make([]string, 0, len(lists))for _, list := range lists {item := strings.TrimSpace(list)if len(item) >= 2 {first := item[0]last := item[len(item)-1]if first == last && first == '"' {r = append(r, strings.TrimSpace(item[1:len(item)-1]))} else {r = append(r, item)}} else {r = append(r, item)}}return r
}
func main() {name := "./csv/export.csv"lines, err := ReadTextFile(name)if err != nil {fmt.Println("读取错误:" + err.Error())return}for _, line := range lines {lists := Filter(strings.Split(line, "\t"))fmt.Println(line + " ==> " + strings.Join(lists, ","))}}