Python Random Access File

前端 未结 7 2129
借酒劲吻你
借酒劲吻你 2020-12-01 16:10

Is there a Python file type for accessing random lines without traversing the whole file? I need to search within a large file, reading the whole thing into memory wouldn\'t

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 17:03

    Yes, you can easily get a random line. Just seek to a random position in the file, then seek towards the beginning until you hit a \n or the beginning of the file, then read a line.

    Code:

    import sys,random
    with open(sys.argv[1],"r") as f:
        f.seek(0,2)                 # seek to end of file
        bytes = f.tell()
        f.seek(int(bytes*random.random()))
    
        # Now seek forward until beginning of file or we get a \n
        while True:
            f.seek(-2,1)
            ch = f.read(1)
            if ch=='\n': break
            if f.tell()==1: break
    
        # Now get a line
        print f.readline()
    

提交回复
热议问题