Splitting large text file into smaller text files by line numbers using Python

前端 未结 7 732
野的像风
野的像风 2020-11-28 08:50

I have a text file say really_big_file.txt that contains:

line 1
line 2
line 3
line 4
...
line 99999
line 100000

I would like to write a Py

相关标签:
7条回答
  • 2020-11-28 09:28
    import csv
    import os
    import re
    
    MAX_CHUNKS = 300
    
    
    def writeRow(idr, row):
        with open("file_%d.csv" % idr, 'ab') as file:
            writer = csv.writer(file, delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL)
            writer.writerow(row)
    
    def cleanup():
        for f in os.listdir("."):
            if re.search("file_.*", f):
                os.remove(os.path.join(".", f))
    
    def main():
        cleanup()
        with open("large_file.csv", 'rb') as results:
            r = csv.reader(results, delimiter=',', quotechar='\"')
            idr = 1
            for i, x in enumerate(r):
                temp = i + 1
                if not (temp % (MAX_CHUNKS + 1)):
                    idr += 1
                writeRow(idr, x)
    
    if __name__ == "__main__": main()
    
    0 讨论(0)
提交回复
热议问题