> It's not better than a generator, but I'm surprised nobody has mentioned the very terse and still mostly readable
> header, *records = [row.strip().split(',') for row in open(filename).readlines()]
Better would be:
header, *records = [row.strip().split(',') for row in open(filename)]
No need to read the lines all into memory first.
Edit: Also if you want to be explicit with the file closing, you could do something like:
with open(filename) as infile:
header, *records = [row.strip().split(',') for row in infile]
That is if we wanted to protect against future changes to semantics for garbage collection/reference counting. I always do this, but I kind of doubt it will ever really matter in any code I write.
> header, *records = [row.strip().split(',') for row in open(filename).readlines()]
Better would be:
No need to read the lines all into memory first.Edit: Also if you want to be explicit with the file closing, you could do something like:
That is if we wanted to protect against future changes to semantics for garbage collection/reference counting. I always do this, but I kind of doubt it will ever really matter in any code I write.