doumei7420 2014-10-20 04:15
浏览 401

如何在GO lang中将大型csv文件拆分为多个文件?

I am a novice Go lang programmer,trying to learn Go lang features.I wanted to split a large csv file into multiple files in GO lang, each file containing the header.How do i do this? I have searched everywhere but couldnt get the right solution.Any help in this regard will be greatly appreciated. Also please suggest me a good book for reference.

Thanking You

  • 写回答

1条回答 默认 最新

  • dougu5847 2014-11-07 03:06
    关注

    Depending on your shell fu this problem might be better suited for common shell utilities but you specifically mentioned go.

    Let's think through the problem.

    How big is this csv file? Are we talking 100 lines or is it 5G ?

    If it's smallish I typically use this:

    http://golang.org/pkg/io/ioutil/#ReadFile

    However, this package also exists:

    http://golang.org/pkg/encoding/csv/

    Regardless - let's return to the abstraction of the problem. You have a header (which is the first line) and then the rest of the document.

    So what we probably want to do (if ignoring csv for the moment) is to read in our file.

    Then we want to split the file body by all the newlines in it.

    You can use this to do so:

    http://golang.org/pkg/strings/#Split

    You didn't mention but do you know how many files you want to split by or would you rather split by the line count or byte count? What's the actual limitation here?

    Generally it's not going to be file count but if we pretend it is we simply want to divide our line count by our expected file count to give lines/file.

    Now we can take slices of the appropriate size and write the file back out via:

    http://golang.org/pkg/io/ioutil/#WriteFile

    A trick I use sometime to help think me threw these things is to write down our mission statement.

    "I want to split a large csv file into multiple files in go"

    Then I start breaking that up into pieces but take the divide/conquer approach - don't try to solve the entire problem in one go - just break it up to where you can think about it.

    Also - make gratiutious use of pseudo-code until you can comfortably write the real code itself. Sometimes it helps to just write a short comment inline with how you think the code should flow and then get it down to the smallest portion that you can code and work from there.

    By the way - many of the golang.org packages have example links where you can literally run in your browser the example code and cut/paste that to your own local environment.

    Also, I know I'll catch some haters with this - but as for books - imo - you are going to learn a lot faster just by trying to get things working rather than reading. Action trumps passivity always. Don't be afraid to fail.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角