Reading a file into a data structure....
Jon Clements
joncle at googlemail.com
Thu Oct 13 19:42:22 EDT 2011
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