Situation: I have a CVD (ClamAV Virus Database) file loaded into RAM using mmap. The format of every line in the CVD file is same as the one of CSV files (':' delimited). Below is a snippet of the code:
def mapping():
with open("main.cvd", 'rt') as f:
global mapper
mapper = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
csv.register_dialect('delimit', delimiter=':', quoting=csv.QUOTE_NONE)
def compare(hashed):
for row in csv.reader(mapper, dialect='delimit'):
if row[1] == hashed:
print('Found!')
Problem: When run, it returns the error _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
Question: How do I read CSV files as text that have been loaded to memory?
Additional information 1: I have tried using StringIO
, it throws the error TypeError: initial_value must be str or None, not mmap.mmap
Additional information 2: I need the file to be in the RAM for faster access to the file and I cannot sacrifice time reading it line by line using functions such as readline()