Reading a file into a data structure....

MrPink tdsimpson at gmail.com
Fri Oct 14 22:59:38 EDT 2011


This is what I have been able to accomplish:

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

The dictionary dDrawings contains records like this:
dDrawings[19971101]
{'pb': '20', 'd': '11/01/1997', 'wb': ['22', '25', '28', '33', '37']}

I am now able to search for ticket in a date range.
keys = dDrawings.keys()
b = [key for key in keys if 20110909 <= key <= 20111212]

How would I search for matching wb (White Balls) in the drawings?

Is there a better way to organize the data so that it will be flexible
enough for different types of searches?
Search by date range, search by pb, search by wb matches, etc.

I hope this all makes sense.

Thanks,

On Oct 13, 7:42 pm, Jon Clements <jon... at googlemail.com> wrote:
> On Oct 13, 10:59 pm,MrPink<tdsimp... at gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > This is a continuing to a post I made in August:http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> > I got some free time to work with Python again and have some followup
> > questions.
>
> > For example, I have a list in a text file like this:
> > Example list of lottery drawings:
> > date,wb,wb,wb,wb,wb,bb
> > 4/1/2011,5,1,45,23,27,27
> > 5/1/2011,15,23,8,48,22,32
> > 6/1/2011,33,49,21,16,34,1
> > 7/1/2011,9,3,13,22,45,41
> > 8/1/2011,54,1,24,39,35,18
> > ....
>
> > Ticket:
> > startdate,enddate,wb,wb,wb,wb,wb,bb
> > 4/1/2011,8/1/2011,5,23,32,21,3,27
>
> > I am trying to determine the optimal way to organize the data
> > structure of the drawing list, search the drawing list, and mark the
> > matches in the drawing list.
>
> > f = open("C:\temp\drawinglist.txt", "r")
> > lines = f.readlines()
> > f.close()
> > drawing = lines[1].split()
>
> > The results in drawing is this:
> > drawing[0] = '4/1/2011'
> > drawing[1] = '5'
> > drawing[2] = '1'
> > drawing[3] = '45'
> > drawing[4] = '23'
> > drawing[5] = '27'
> > drawing[6] = '27'
>
> > I need to convert drawing[0] to a date datatype.  This works, but I'm
> > sure there is a better way.
> > from datetime import date
> > month, day, year = drawing[0].split('/')
> > drawing[0] = date(int(year), int(month), int(day))
>
> > For searching, I need to determine if the date of the drawing is
> > within the date range of the ticket.  If yes, then mark which numbers
> > in the drawing match the numbers in the ticket.
>
> > ticket[0] = '4/1/2011'
> > ticket[0] = '8/1/2011'
> > ticket[0] = '5'
> > ticket[0] = '23'
> > ticket[0] = '32'
> > ticket[0] = '21'
> > ticket[0] = '3'
> > ticket[0] = 27'
>
> > drawing[0] = '4/1/2011' (match)
> > drawing[1] = '5' (match)
> > drawing[2] = '1'
> > drawing[3] = '45'
> > drawing[4] = '23' (match)
> > drawing[5] = '27'
> > drawing[6] = '27' (match)
>
> > I'm debating on structuring the drawing list like this:
> > drawing[0] = '4/1/2011'
> > drawing[1][0] = '5'
> > drawing[1][1] = '1'
> > drawing[1][2] = '45'
> > drawing[1][3] = '23'
> > drawing[1][4] = '27'
> > drawing[2] = '27'
>
> > Sort drawing[1] from low to high
> > drawing[1][0] = '1'
> > drawing[1][1] = '5'
> > drawing[1][2] = '23'
> > drawing[1][3] = '27'
> > drawing[1][4] = '45'
>
> > I want to keep the drawing list in memory for reuse.
>
> > Any guidance would be most helpful and appreciated.
> > BTW, I want to learn, so be careful not to do too much of the work for
> > me.
> > I'm using WingIDE to do my work.
>
> > Thanks,
>
> - Use the csv module to read the file
> - Use strptime to process the date field
> - Use a set for draw numbers (you'd have to do pure equality on the
> bb)
> - Look at persisting in a sqlite3 DB (maybe with a custom convertor)
>
> hth,
>
> Jon.




More information about the Python-list mailing list