How do quickly search through a .csv file in Python

后端 未结 6 985
别跟我提以往
别跟我提以往 2020-12-18 09:10

I\'m reading a 6 million entry .csv file with Python, and I want to be able to search through this file for a particular entry.

Are there any tricks to search the en

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 09:45

    you can use memory mapping for really big files

    import mmap,os,re
    reportFile = open( "big_file" )
    length = os.fstat( reportFile.fileno() ).st_size
    try:
        mapping = mmap.mmap( reportFile.fileno(), length, mmap.MAP_PRIVATE, mmap.PROT_READ )
    except AttributeError:
        mapping = mmap.mmap( reportFile.fileno(), 0, None, mmap.ACCESS_READ )
    data = mapping.read(length)
    pat =re.compile("b.+",re.M|re.DOTALL) # compile your pattern here.
    print pat.findall(data)
    

提交回复
热议问题