You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

csv.go 1.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package util
  15. import (
  16. "bufio"
  17. "encoding/csv"
  18. "io"
  19. "os"
  20. )
  21. func LoadCsvFile(path string, rows *[][]string) {
  22. file, err := os.Open(path)
  23. if err != nil {
  24. panic(err)
  25. }
  26. defer file.Close()
  27. reader := csv.NewReader(bufio.NewReader(file))
  28. reader.LazyQuotes = true
  29. i := 0
  30. for {
  31. line, err := reader.Read()
  32. if err == io.EOF {
  33. break
  34. } else if err != nil {
  35. // log.Fatal(error)
  36. panic(err)
  37. }
  38. *rows = append(*rows, line)
  39. i += 1
  40. }
  41. }
  42. func WriteCsvFile(path string, rows *[][]string) {
  43. file, err := os.Create(path)
  44. if err != nil {
  45. panic(err)
  46. }
  47. defer file.Close()
  48. writer := csv.NewWriter(file)
  49. defer writer.Flush()
  50. i := 0
  51. for _, row := range *rows {
  52. err = writer.Write(row)
  53. if err != nil {
  54. panic(err)
  55. }
  56. i += 1
  57. }
  58. }