drdu53813 2018-04-02 19:41
浏览 420

如何读取大型CSV文件

What is the best way to read large CSV files, at the moment I am reading one record at a time rather than using ReadAll().

reader := csv.NewReader(csvFile)
reader.FieldsPerRecord = -1

for {
    // read just one record at a time
    record, err := reader.Read()
    if err == io.EOF {
        break
    } else if err != nil {
        checkErr(err)
        return
    }

Is there a better way to do this to save memory?

I am writing each record/row to a database by sending an array over GRPC to a separate service.

  • 写回答

1条回答 默认 最新

  • dongyili5843 2018-04-02 21:04
    关注

    Yes, there is one option you can use to improve it.
    It is possible to allow reader to reuse a slice that is returned by it on each Read method call.
    To do it you need to set reader.ReuseRecord = true.
    But be careful, because the returned slice may be changed after the next call of Read!

    评论

报告相同问题?

悬赏问题

  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题