[CentralOH] Improving Zipfile Code
eric at intellovations.com
Mon Oct 22 15:27:13 CEST 2012
The biggest thing that smells to me is the magic number '2'. I assume
that's because you are assuming a header row since you are opening a
DictReader, but still want to get the line number of the csv row.
Thankfully, csv readers expose the 'line_num' property, so you can get the
line number of the file directly.
Also, while not 'smelly', the anonymous function (the lambda) can be
cleaned up by using the standard library module fnmatch, which is
specifically built for file matching.
You can also break up your combo opening the zip file and creating a
DictReader in the for loop, which doesn't do anything other than making it
look a little cleaner and makes it clearer at a glance what's going on.
Finally, since 'rU' is something you need to look up to understand, I put a
comment so it makes it clear what the 'rU' is for.
With those revisions, here is the updated code:
from zipfile import ZipFile
z = ZipFile(zipfilename)
for filename in fnmatch.filter(z.namelist(), '*.xyz'):
csvfile = z.open(filename, 'rU') # Open with universal newline
csvreader = csv.DictReader(csvfile)
for row in csvreader:
print 'line_number:', csvreader.line_num, 'row:', row
On Sun, Oct 21, 2012 at 10:46 PM, <jep200404 at columbus.rr.com> wrote:
> The following code smells. How would you improve it?
> import csv
> from zipfile import ZipFile
> def foo(filename):
> z = ZipFile(filename)
> for f in filter((lambda s: s.endswith('.xyz')), z.namelist()):
> for line_number, row in enumerate(csv.DictReader(z.open(f, 'rU')),
> print 'line_number:', line_number, 'row:', row
> CentralOH mailing list
> CentralOH at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the CentralOH