代码之家  ›  专栏  ›  技术社区  ›  twhale

如何使用Python拆分大型文档?

  •  -1
  • twhale  · 技术社区  · 7 年前

    我想分一长串 text.csv 文本文件大小为2 x 400000,分为80个较小的文件(2 x 6000)。理想情况下,我希望将每个较小的文件以text1.csv、text2.csv等格式写入磁盘。最有效的方法是什么?

             col 1         col2
    0        text          text
    1        text          text
    2        text          text
    ...
    399999   text          text
    400000   text          text
    

    较小的文件如下所示:

             col 1         col2
    0        text          text
    1        text          text
    2        text          text
    ...
    4999     text          text
    5000     text          text
    
    
             col 1         col2
    5001     text          text
    5002     text          text
    5003     text          text
    ...
    9999     text          text
    10000    text          text
    
    etc.
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   rje    7 年前

    我自己不会为此编写任何东西,而是在shell上使用split命令——或者可能将其作为python的外部程序调用。请参见:

    How to split a large text file into smaller files with equal number of lines?