Fastest Way to read CSV to list of tuples with condition/filter and column type assignment? (Python)

Viewed 42

Hi Everyone and thanks in advance for your help.

I need to read a csv to a list of tuples while conditioning the list on a value (>=0.75) and changing the columns to different typing. Please Note you can not!! use pandas, NO PANDAS

I'm trying to figure out how to do it the FASTEST method possible.

This is how I did it (put i think it is not efficient):

def load_csv_to_list(path):
  with open(path) as csv_file:
    table = list(reader(csv_file))
  lst = [table[0]]
  count = 0
  for row in table[1:]:
    if float(row[2]) >= 0.75:
      date = datetime.strptime(row[0], "%d/%m/%Y").strftime("%d/%m/%Y")
      row = (date,int(row[1]),float(row[2]))
      lst.append(row)
  return (lst)

start = timeit.timeit()
load_csv_to_list(path)
end = timeit.timeit()
print(start - end)

answer : 0.00013872199997422285

0 Answers
Related