1
0

feat: 删除不需要文件夹

This commit is contained in:
TsMask
2023-10-10 10:56:44 +08:00
parent ce7c3cae68
commit d173205528
154 changed files with 32276 additions and 1 deletions

88
lib/core/file/csv.go Normal file
View File

@@ -0,0 +1,88 @@
package file
import (
"encoding/csv"
"os"
"path/filepath"
"strings"
"ems.agt/lib/log"
)
// 写入CSV文件需要转换数据
// 例如:
// data := [][]string{}
// data = append(data, []string{"姓名", "年龄", "城市"})
// data = append(data, []string{"1", "2", "3"})
// err := file.WriterCSVFile(data, filePath)
func WriterCSVFile(data [][]string, filePath string) error {
// 获取文件所在的目录路径
dirPath := filepath.Dir(filePath)
// 确保文件夹路径存在
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
log.Errorf("创建文件夹失败 CreateFile %v", err)
}
// 创建或打开文件
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()
// 创建CSV编写器
writer := csv.NewWriter(file)
defer writer.Flush()
// 写入数据
for _, row := range data {
writer.Write(row)
}
return nil
}
// 读取CSV文件转换map数据
func ReadCSVFile(filePath string) []map[string]string {
// 创建 map 存储 CSV 数据
arr := make([]map[string]string, 0)
// 打开 CSV 文件
file, err := os.Open(filePath)
if err != nil {
log.Fatal("无法打开 CSV 文件:", err)
return arr
}
defer file.Close()
// 创建 CSV Reader
reader := csv.NewReader(file)
// 读取 CSV 头部行
header, err := reader.Read()
if err != nil {
log.Fatal("无法读取 CSV 头部行:", err)
return arr
}
// 遍历 CSV 数据行
for {
// 读取一行数据
record, err := reader.Read()
if err != nil {
// 到达文件末尾或遇到错误时退出循环
break
}
// 将 CSV 数据插入到 map 中
data := make(map[string]string)
for i, value := range record {
key := strings.ToLower(header[i])
data[key] = value
}
arr = append(arr, data)
}
return arr
}