dpii73380 2018-10-20 20:12
浏览 60
已采纳

Go中的CSV解析器无法写入文件

I'm coding a simple tool to help myself at work but it's mostly for learning purposes, this tool takes in a ".csv" file and converts the data in the file to sql insert into statements, then write the statements to a ".sql" file. The problem is that after it's done, when I check the file, the last row is never complete, usually it stops

INSERT INTO tablename (field1, field2, field3, field4

I'm using bufio.NewWriter, and the function WriteString, and it's not returning any error, so I have no idea what's happening, the full ".csv" file I'm testing on is 8Kb, but I can't share it because it contains sensible info sorry.

Another problem I'm having is with this particular statement

sqlFile, err := os.Create(date.Format("20-11-2018 20.10.05") + ".sql")

What I try to do here is to put a timestamp as the name of the file with the format "dd-MM-yyyy hh.mm.ss" but the file names end up like this "200-1010-20108 200.100.34.sql", no idea what's happening here.

Before anyone tells me, yes I'm sure there are already tools for this, but a big part of the motivation to do this is to learn, so I welcome any idea or criticism to the entire code and not just the parts I'm having problems with.

Link to the github page

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongmen5867 2018-10-20 20:38
    关注

    Package bufio

    import "bufio" 
    

    func (*Writer) Flush

    func (b *Writer) Flush() error
    

    Flush writes any buffered data to the underlying io.Writer.

    After you have finished writing, flush the buffered writer before closing the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置