mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(fileutil): preserve CSV fields and truncate replaced files (#378)
This commit is contained in:
1 parent
778bd2e14f
commit
c701a35586
2 files changed
+32
-20
No files matched your search
+2
-20
@@ -761,6 +761,8 @@ func WriteCsvFile(filepath string, records [][]string, append bool, delimiter ..
|
||||
|
||||
if append {
|
||||
flag = flag | os.O_APPEND
|
||||
} else {
|
||||
flag = flag | os.O_TRUNC
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(filepath, flag, 0644)
|
||||
@@ -778,13 +780,6 @@ func WriteCsvFile(filepath string, records [][]string, append bool, delimiter ..
|
||||
writer.Comma = ','
|
||||
}
|
||||
|
||||
// 遍历所有记录并处理包含分隔符或双引号的单元格
|
||||
for i := range records {
|
||||
for j := range records[i] {
|
||||
records[i][j] = escapeCSVField(records[i][j], writer.Comma)
|
||||
}
|
||||
}
|
||||
|
||||
return writer.WriteAll(records)
|
||||
}
|
||||
|
||||
@@ -843,19 +838,6 @@ func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// escapeCSVField 处理单元格内容,如果包含分隔符,则用双引号包裹
|
||||
func escapeCSVField(field string, delimiter rune) string {
|
||||
// 替换所有的双引号为两个双引号
|
||||
escapedField := strings.ReplaceAll(field, "\"", "\"\"")
|
||||
|
||||
// 如果字段包含分隔符、双引号或换行符,用双引号包裹整个字段
|
||||
if strings.ContainsAny(escapedField, string(delimiter)+"\"\n") {
|
||||
escapedField = fmt.Sprintf("\"%s\"", escapedField)
|
||||
}
|
||||
|
||||
return escapedField
|
||||
}
|
||||
|
||||
// WriteMapsToCsv write slice of map to csv file.
|
||||
// Play: https://go.dev/play/p/umAIomZFV1c
|
||||
// filepath: Path to the CSV file.
|
||||
|
||||
Reference in new issue
Block a user