Reading a file into a data structure....

Chris Angelico rosuav at gmail.com
Sat Oct 15 00:30:44 EDT 2011


On Sat, Oct 15, 2011 at 1:59 PM, MrPink <tdsimpson at gmail.com> wrote:
> def isInt(s):
>    try:
>        i = int(s)
>        return True
>    except ValueError:
>        return False
>
> f = open("powerball.txt", "r")
> lines = f.readlines()
> f.close()
>
> dDrawings = {}
> for line in lines:
>    if isInt(line[0]):
>        t = line.split()
>        d = t[0]
>        month,day,year = t[0].split("/")
>        i = int(year + month + day)
>        wb = t[1:6]
>        wb.sort()
>        pb = t[6]
>        r = {'d':d,'wb':wb,'pb':pb}
>        dDrawings[i] = r
>

Here's a quick rejig:

dDrawings = {}
for line in open("powerball.txt"):
   try:
       t = line.split()
       d = t[0]
       month,day,year = t[0].split("/")
       i = int(year + month + day)
       wb = t[1:6]
       wb.sort()
       pb = t[6]
       r = {'d':d,'wb':wb,'pb':pb}
       dDrawings[i] = r
   except ValueError:
       pass

There are two significant differences. One is that the file is kept
open until processing is complete, rather than reading the file into a
list and then iterating over the list. Your processing is pretty
simple, so it's unlikely to make a difference, but if you're doing a
lengthy operation on the lines of text, or conversely if you're
reading in gigs and gigs of file, you may want to take that into
consideration. The other change is that a ValueError _anywhere_ in
processing will cause the line to be silently ignored. If this isn't
what you want, then shorten the try/except block and make it use
'continue' instead of 'pass' (which will then silently ignore that
line, but leave the rest of processing unguarded by try/except).

The most likely cause of another ValueError is this line:
       month,day,year = t[0].split("/")
If there are not precisely two slashes, this will:

>>> a,b,c="asdf/qwer".split("/")
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    a,b,c="asdf/qwer".split("/")
ValueError: need more than 2 values to unpack

Do you want this to cause the line to be ignored, or to noisily abort
the whole script?

ChrisA



More information about the Python-list mailing list