我的 Golang 程序会将第一个输入内容打印两次到文件中。

huangapple go评论108阅读模式
英文:

My program in Golang prints the first input two times in the file

问题

我尝试获取一些以CSV格式的字符串作为输入,然后将其打印到实际的CSV文件中。它可以工作,但是会将第一个字符串打印两次。

我的代码如下:

  1. func main() {
  2. scanner := bufio.NewScanner(os.Stdin)
  3. n := 0
  4. inputFile, err := os.Create("input.csv") // 创建input.csv文件
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. csvwriter := csv.NewWriter(inputFile)
  9. fmt.Println("How many records?")
  10. fmt.Scanln(&n)
  11. fmt.Println("Enter the records")
  12. var lines [][]string
  13. for i := 0; i < n; i++ {
  14. scanner.Scan()
  15. text := scanner.Text()
  16. lines = append(lines, []string{text})
  17. err := csvwriter.WriteAll(lines)
  18. if err != nil {
  19. return
  20. }
  21. }
  22. csvwriter.Flush()
  23. inputFile.Close()
  24. }

对于n=2和记录:

  1. abcd, efgh, ijklmn
  2. opq, rstu, vwxyz

输出结果如下:

  1. "abcd, efgh, ijklmn"
  2. "abcd, efgh, ijklmn"
  3. "opq, rstu, vwxyz"

这是我第一次使用Golang,有点迷茫 我的 Golang 程序会将第一个输入内容打印两次到文件中。

英文:

I try to get some CSV formatted string as input and then to print it to an actual CSV file. It works but it prints the first string 2 times.

My code looks like this:

  1. func main() {
  2. scanner := bufio.NewScanner(os.Stdin)
  3. n := 0
  4. inputFile, err := os.Create(&quot;input.csv&quot;) //create the input.csv file
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. csvwriter := csv.NewWriter(inputFile)
  9. fmt.Println(&quot;How many records ?&quot;)
  10. fmt.Scanln(&amp;n)
  11. fmt.Println(&quot;Enter the records&quot;)
  12. var lines [][]string
  13. for i := 0; i &lt; n; i++ {
  14. scanner.Scan()
  15. text := scanner.Text()
  16. lines = append(lines, []string{text})
  17. err := csvwriter.WriteAll(lines)
  18. if err != nil {
  19. return
  20. }
  21. }
  22. csvwriter.Flush()
  23. inputFile.Close()
  24. }

for n=2 and the records:

  1. abcd, efgh, ijklmn
  2. opq, rstu, vwxyz

the output looks like this:

  1. &quot;abcd, efgh, ijklmn&quot;
  2. &quot;abcd, efgh, ijklmn&quot;
  3. &quot;opq, rstu, vwxyz&quot;

It is my first time working with Golang and I am a little bit lost 我的 Golang 程序会将第一个输入内容打印两次到文件中。

答案1

得分: 1

csvwriter.WriteAll(lines) WriteAll 使用 Write 将多个 CSV 记录写入 w,然后调用 Flush,返回 Flush 的任何错误。

每次在循环中读取时,您都会将行追加到文件中并刷新。

  1. func main() {
  2. scanner := bufio.NewScanner(os.Stdin)
  3. n := 0
  4. inputFile, err := os.Create("input.csv") // 创建 input.csv 文件
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. defer inputFile.Close()
  9. csvwriter := csv.NewWriter(inputFile)
  10. fmt.Println("How many records?")
  11. fmt.Scanln(&n)
  12. fmt.Println("Enter the records")
  13. var lines [][]string
  14. for i := 0; i < n; i++ {
  15. scanner.Scan()
  16. text := scanner.Text()
  17. lines = append(lines, []string{text})
  18. }
  19. err = csvwriter.WriteAll(lines)
  20. if err != nil {
  21. return
  22. }
  23. }
英文:

csvwriter.WriteAll(lines) WriteAll writes multiple CSV records to w using Write and then calls Flush, returning any error from the Flush.

You are appending lines every time you read in a loop and flushing to the file.

  1. func main() {
  2. scanner := bufio.NewScanner(os.Stdin)
  3. n := 0
  4. inputFile, err := os.Create(&quot;input.csv&quot;) //create the input.csv file
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. defer inputFile.Close()
  9. csvwriter := csv.NewWriter(inputFile)
  10. fmt.Println(&quot;How many records ?&quot;)
  11. fmt.Scanln(&amp;n)
  12. fmt.Println(&quot;Enter the records&quot;)
  13. var lines [][]string
  14. for i := 0; i &lt; n; i++ {
  15. scanner.Scan()
  16. text := scanner.Text()
  17. lines = append(lines, []string{text})
  18. }
  19. err = csvwriter.WriteAll(lines)
  20. if err != nil {
  21. return
  22. }
  23. }

答案2

得分: 0

你在循环中写入csv文件,导致第一行打印了两次。以下是已更正的代码。

  1. package main
  2. import (
  3. "bufio"
  4. "encoding/csv"
  5. "fmt"
  6. "log"
  7. "os"
  8. )
  9. func main() {
  10. scanner := bufio.NewScanner(os.Stdin)
  11. n := 0
  12. inputFile, err := os.Create("input.csv") // 创建input.csv文件
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. defer func() {
  17. inputFile.Close()
  18. }()
  19. csvwriter := csv.NewWriter(inputFile)
  20. defer func() {
  21. csvwriter.Flush()
  22. }()
  23. fmt.Println("有多少条记录?")
  24. fmt.Scanln(&n)
  25. fmt.Println("输入记录")
  26. var lines [][]string
  27. for i := 0; i < n; i++ {
  28. scanner.Scan()
  29. text := scanner.Text()
  30. lines = append(lines, []string{text})
  31. }
  32. err = csvwriter.WriteAll(lines)
  33. if err != nil {
  34. return
  35. }
  36. }

希望对你有帮助!

英文:

You were writing the csv in loop so that first line printed double. Here is the corrected code.

  1. package main
  2. import (
  3. &quot;bufio&quot;
  4. &quot;encoding/csv&quot;
  5. &quot;fmt&quot;
  6. &quot;log&quot;
  7. &quot;os&quot;
  8. )
  9. func main() {
  10. scanner := bufio.NewScanner(os.Stdin)
  11. n := 0
  12. inputFile, err := os.Create(&quot;input.csv&quot;) //create the input.csv file
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. defer func() {
  17. inputFile.Close()
  18. }()
  19. csvwriter := csv.NewWriter(inputFile)
  20. defer func() {
  21. csvwriter.Flush()
  22. }()
  23. fmt.Println(&quot;How many records ?&quot;)
  24. fmt.Scanln(&amp;n)
  25. fmt.Println(&quot;Enter the records&quot;)
  26. var lines [][]string
  27. for i := 0; i &lt; n; i++ {
  28. scanner.Scan()
  29. text := scanner.Text()
  30. lines = append(lines, []string{text})
  31. }
  32. err = csvwriter.WriteAll(lines)
  33. if err != nil {
  34. return
  35. }
  36. }

huangapple
  • 本文由 发表于 2022年11月26日 07:08:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/74578519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定