How do I read a random line from one file?

前端 未结 11 799
灰色年华
灰色年华 2020-12-04 20:03

Is there a built-in method to do it? If not how can I do this without costing too much overhead?

11条回答
  •  情歌与酒
    2020-12-04 20:29

    If you don't want to read over the entire file, you can seek into the middle of the file, then seek backwards for the newline, and call readline.

    Here is a Python3 script which does just this,

    One disadvantage with this method is short lines have lower likelyhood of showing up.

    def read_random_line(f, chunk_size=16):
        import os
        import random
        with open(f, 'rb') as f_handle:
            f_handle.seek(0, os.SEEK_END)
            size = f_handle.tell()
            i = random.randint(0, size)
            while True:
                i -= chunk_size
                if i < 0:
                    chunk_size += i
                    i = 0
                f_handle.seek(i, os.SEEK_SET)
                chunk = f_handle.read(chunk_size)
                i_newline = chunk.rfind(b'\n')
                if i_newline != -1:
                    i += i_newline + 1
                    break
                if i == 0:
                    break
            f_handle.seek(i, os.SEEK_SET)
            return f_handle.readline()
    

提交回复
热议问题