Python how to read N number of lines at a time

后端 未结 6 1134
感情败类
感情败类 2020-11-27 03:42

I am writing a code to take an enormous textfile (several GB) N lines at a time, process that batch, and move onto the next N lines until I have completed the entire file.

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 03:45

    Assuming "batch" means to want to process all 16 recs at one time instead of individually, read the file one record at a time and update a counter; when the counter hits 16, process that group.

    interim_list = []
    infile = open("my_very_large_text_file", "r")
    ctr = 0
    for rec in infile:
        interim_list.append(rec)
        ctr += 1
        if ctr > 15:
            process_list(interim_list)
            interim_list = []
            ctr = 0

    the final group

    process_list(interim_list)

提交回复
热议问题