How to read specific part of large file in Python

后端 未结 2 1850
一整个雨季
一整个雨季 2020-11-27 21:59

Given a large file (hundreds of MB) how would I use Python to quickly read the content between a specific start and end index within the file?

Essentially, I\'m look

2条回答
  •  悲&欢浪女
    2020-11-27 22:15

    This is my solution with variable width encoding. My CSV file contains a dictionary where each row is a new item.

    def get_stuff(filename, count, start_index):
        with open(filename, 'r') as infile:
                 reader = csv.reader(infile)
                 num = 0 
                 for idx, row in enumerate(reader):
                     if idx >= start_index-1:
                         if num >= count:
                             return
                     else:
                         yield row 
                         num += 1
    

提交回复
热议问题