mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 09:12:28 +08:00
feat: add WirteCsvFile
This commit is contained in:
109
docs/fileutil.md
109
docs/fileutil.md
@@ -43,6 +43,7 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteCsvFile](#WriteCsvFile)
|
||||||
- [WriteStringToFile](#WriteStringToFile)
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
- [WriteBytesToFile](#WriteBytesToFile)
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
@@ -503,7 +504,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsZipFile">IsZipFile</span>
|
### <span id="IsZipFile">IsZipFile</span>
|
||||||
|
|
||||||
<p>Checks if file is zip file or not.</p>
|
<p>Checks if file is zip file or not.</p>
|
||||||
@@ -664,6 +664,42 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="WriteCsvFile">WriteCsvFile</span>
|
||||||
|
|
||||||
|
<p>Write content to target csv file.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteCsvFile(filepath string, records [][]string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
data := [][]string{
|
||||||
|
{"Lili", "22", "female"},
|
||||||
|
{"Jim", "21", "male"},
|
||||||
|
}
|
||||||
|
err := WriteCsvFile("./testdata/test2.csv", data, false)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
content, _ := ReadCsvFile("./testdata/test2.csv")
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// [[Lili 22 female] [Jim 21 male]]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
@@ -688,33 +724,32 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
filepath := "./bytes.txt"
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
file, err := os.Create(filepath)
|
file, err := os.Create(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := fileutil.ReadFileToString(filepath)
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove(filepath)
|
os.Remove(filepath)
|
||||||
|
|
||||||
fmt.Println(content)
|
fmt.Println(content)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="WriteStringToFile">WriteStringToFile</span>
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
<p>Writes string to target file.</p>
|
<p>Writes string to target file.</p>
|
||||||
@@ -738,28 +773,28 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
filepath := "./test.txt"
|
filepath := "./test.txt"
|
||||||
|
|
||||||
file, err := os.Create(filepath)
|
file, err := os.Create(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := fileutil.ReadFileToString(filepath)
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove(filepath)
|
os.Remove(filepath)
|
||||||
|
|
||||||
fmt.Println(content)
|
fmt.Println(content)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ import (
|
|||||||
- [MTime](#MTime)
|
- [MTime](#MTime)
|
||||||
- [Sha](#Sha)
|
- [Sha](#Sha)
|
||||||
- [ReadCsvFile](#ReadCsvFile)
|
- [ReadCsvFile](#ReadCsvFile)
|
||||||
|
- [WriteCsvFile](#WriteCsvFile)
|
||||||
- [WriteStringToFile](#WriteStringToFile)
|
- [WriteStringToFile](#WriteStringToFile)
|
||||||
- [WriteBytesToFile](#WriteBytesToFile)
|
- [WriteBytesToFile](#WriteBytesToFile)
|
||||||
|
|
||||||
@@ -503,7 +504,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsZipFile">IsZipFile</span>
|
### <span id="IsZipFile">IsZipFile</span>
|
||||||
|
|
||||||
<p>判断文件是否是zip压缩文件。</p>
|
<p>判断文件是否是zip压缩文件。</p>
|
||||||
@@ -664,6 +664,43 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="WriteCsvFile">WriteCsvFile</span>
|
||||||
|
|
||||||
|
<p>向csv文件写入内容。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WriteCsvFile(filepath string, records [][]string, append bool) error
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/fileutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
data := [][]string{
|
||||||
|
{"Lili", "22", "female"},
|
||||||
|
{"Jim", "21", "male"},
|
||||||
|
}
|
||||||
|
err := WriteCsvFile("./testdata/test2.csv", data, false)
|
||||||
|
fmt.Println(err)
|
||||||
|
|
||||||
|
content, _ := ReadCsvFile("./testdata/test2.csv")
|
||||||
|
fmt.Println(content)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// <nil>
|
||||||
|
// [[Lili 22 female] [Jim 21 male]]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
### <span id="WriteBytesToFile">WriteBytesToFile</span>
|
||||||
|
|
||||||
<p>将bytes写入文件。</p>
|
<p>将bytes写入文件。</p>
|
||||||
@@ -687,33 +724,32 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
filepath := "./bytes.txt"
|
filepath := "./bytes.txt"
|
||||||
|
|
||||||
file, err := os.Create(filepath)
|
file, err := os.Create(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
err = fileutil.WriteBytesToFile(filepath, []byte("hello"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := fileutil.ReadFileToString(filepath)
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove(filepath)
|
os.Remove(filepath)
|
||||||
|
|
||||||
fmt.Println(content)
|
fmt.Println(content)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="WriteStringToFile">WriteStringToFile</span>
|
### <span id="WriteStringToFile">WriteStringToFile</span>
|
||||||
|
|
||||||
<p>将字符串写入文件。</p>
|
<p>将字符串写入文件。</p>
|
||||||
@@ -737,28 +773,28 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
filepath := "./test.txt"
|
filepath := "./test.txt"
|
||||||
|
|
||||||
file, err := os.Create(filepath)
|
file, err := os.Create(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
err = fileutil.WriteStringToFile(filepath, "hello", true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content, err := fileutil.ReadFileToString(filepath)
|
content, err := fileutil.ReadFileToString(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove(filepath)
|
os.Remove(filepath)
|
||||||
|
|
||||||
fmt.Println(content)
|
fmt.Println(content)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -370,7 +370,7 @@ func MTime(filepath string) (int64, error) {
|
|||||||
return f.ModTime().Unix(), nil
|
return f.ModTime().Unix(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MTime returns file sha value, param `shaType` should be 1, 256 or 512.
|
// Sha returns file sha value, param `shaType` should be 1, 256 or 512.
|
||||||
func Sha(filepath string, shaType ...int) (string, error) {
|
func Sha(filepath string, shaType ...int) (string, error) {
|
||||||
file, err := os.Open(filepath)
|
file, err := os.Open(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -420,6 +420,27 @@ func ReadCsvFile(filepath string) ([][]string, error) {
|
|||||||
return records, nil
|
return records, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteCsvFile write content to target csv file.
|
||||||
|
func WriteCsvFile(filepath string, records [][]string, append bool) error {
|
||||||
|
flag := os.O_RDWR | os.O_CREATE
|
||||||
|
|
||||||
|
if append {
|
||||||
|
flag = flag | os.O_APPEND
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.OpenFile(filepath, flag, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
writer := csv.NewWriter(f)
|
||||||
|
writer.Comma = ','
|
||||||
|
|
||||||
|
return writer.WriteAll(records)
|
||||||
|
}
|
||||||
|
|
||||||
// WriteStringToFile write string to target file.
|
// WriteStringToFile write string to target file.
|
||||||
func WriteStringToFile(filepath string, content string, append bool) error {
|
func WriteStringToFile(filepath string, content string, append bool) error {
|
||||||
flag := os.O_RDWR | os.O_CREATE
|
flag := os.O_RDWR | os.O_CREATE
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ func TestSha(t *testing.T) {
|
|||||||
func TestReadCsvFile(t *testing.T) {
|
func TestReadCsvFile(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestReadCsvFile")
|
assert := internal.NewAssert(t, "TestReadCsvFile")
|
||||||
|
|
||||||
content, err := ReadCsvFile("./testdata/test.csv")
|
content, err := ReadCsvFile("./testdata/demo.csv")
|
||||||
t.Log(content)
|
t.Log(content)
|
||||||
|
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
@@ -278,6 +278,27 @@ func TestReadCsvFile(t *testing.T) {
|
|||||||
assert.Equal("Bob", content[0][0])
|
assert.Equal("Bob", content[0][0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteCsvFile(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestWriteCsvFile")
|
||||||
|
|
||||||
|
csvFilePath := "./testdata/test1.csv"
|
||||||
|
content := [][]string{
|
||||||
|
{"Lili", "22", "female"},
|
||||||
|
{"Jim", "21", "male"},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := WriteCsvFile(csvFilePath, content, false)
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
readContent, err := ReadCsvFile(csvFilePath)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
assert.Equal(2, len(readContent))
|
||||||
|
assert.Equal(3, len(readContent[0]))
|
||||||
|
assert.Equal("Lili", content[0][0])
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteStringToFile(t *testing.T) {
|
func TestWriteStringToFile(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestWriteStringToFile")
|
assert := internal.NewAssert(t, "TestWriteStringToFile")
|
||||||
|
|
||||||
|
|||||||
2
fileutil/testdata/test1.csv
vendored
Normal file
2
fileutil/testdata/test1.csv
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Lili,22,female
|
||||||
|
Jim,21,male
|
||||||
|
Reference in New Issue
Block a user