Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WriteCsvFile自定义分割符,增加map切片写入csv,增加追踪函数运行时间的函数 #157

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,12 @@ func TimestampNano(timezone ...string) int64 {

return t.UnixNano()
}

// TraceFuncTime: trace the func costed time,just call it at top of the func like `defer TraceFuncTime()()`
func TraceFuncTime() func() {
pre := time.Now()
return func() {
elapsed := time.Since(pre)
fmt.Println("Costs Time:\t", elapsed)
}
}
63 changes: 61 additions & 2 deletions fileutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,10 @@ func ReadCsvFile(filepath string) ([][]string, error) {
}

// WriteCsvFile write content to target csv file.
// append: append to existing csv file
// delimiter: specifies csv delimiter
// Play: https://go.dev/play/p/dAXm58Q5U1o
func WriteCsvFile(filepath string, records [][]string, append bool) error {
func WriteCsvFile(filepath string, records [][]string, append bool, delimiter ...rune) error {
flag := os.O_RDWR | os.O_CREATE

if append {
Expand All @@ -587,7 +589,19 @@ func WriteCsvFile(filepath string, records [][]string, append bool) error {
defer f.Close()

writer := csv.NewWriter(f)
writer.Comma = ','
// 设置默认分隔符为逗号,除非另外指定
if len(delimiter) > 0 {
writer.Comma = delimiter[0]
} else {
writer.Comma = ','
}

// 遍历所有记录并处理包含分隔符或双引号的单元格
for i := range records {
for j := range records[i] {
records[i][j] = escapeCSVField(records[i][j], writer.Comma)
}
}

return writer.WriteAll(records)
}
Expand Down Expand Up @@ -646,3 +660,48 @@ func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error) {
return nil, func() {}, errors.New("unknown file type")
}
}

// 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
}

// map切片写入csv文件中
func WriteMapsToCSV(filepath string, records []map[string]string, append_to_existing_file bool, delimiter ...rune) error {
var datas_to_write [][]string
// 标题(列名)
var headers []string
if len(records) > 0 {
for key := range records[0] {
headers = append(headers, key)
}
}
// 追加模式不重复写字段名
if !append_to_existing_file {
datas_to_write = append(datas_to_write, headers)
}
// 写入数据行
for _, record := range records {
var row []string
for _, header := range headers {
row = append(row, record[header])
}
datas_to_write = append(datas_to_write, row)
}
// 提取自定义分隔符
var sep rune
if len(delimiter) > 0 {
sep = delimiter[0]
} else {
sep = ','
}
return WriteCsvFile(filepath, datas_to_write, append_to_existing_file, sep)
}
Loading