[Tutor] ashamed

Moedeloos Overste kloosterjunkie at hotmail.com
Mon Nov 27 21:32:05 CET 2006


I shamefully bowe my head. How stupid of me not to think of that. I assumed 
that as the script is in a certain directory the output would also be in 
that directory. A very humbling learning experience....

Thank you for the pointer on file open/close outside of the loop. That 
should speed things up

Right now I'm kept busy by dictionaries. I've created a dictionary 
containing all possible keys(lottery numbers) with their values set to zero. 
Now I want read the file and for each time a key is in the file i want it's 
value to go up +1. Like a counter. So far I've not been succesfull:-) see 
code.



d={1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0, 8:0, 9:0, 10:0, 11:0, 12:0, 13:0, 
14:0, 15:0,
    16:0, 17:0, 18:0, 19:0, 20:0, 21:0, 22:0, 23:0, 24:0, 25:0, 26:0, 27:0, 
28:0,
    29:0, 30:0, 31:0, 32:0, 33:0, 34:0, 35:0, 36:0, 37:0, 38:0, 39:0, 40:0, 
41:0, 42:0,
    43:0, 44:0, 45:0}

done=0
fd = open("draw__output.txt",'r')
while not done:
    line = fd.readline()
    if line == '':
        done = 1
    else:
       for i in line:
          d[i] = int(d[i])+ 1




>From: "Alan Gauld" <alan.gauld at btinternet.com>
>To: tutor at python.org
>Subject: Re: [Tutor] Newbie question: join() string method
>Date: Mon, 27 Nov 2006 18:46:04 -0000
>
>"Moedeloos Overste" <kloosterjunkie at hotmail.com> wrote
>
> > One question: When I run the program from IDLE it writes the data to
> > file
> > but when I run it from the command prompt(win32)  it doesn't. why is
> > this?
>
>How do you know? Have you searched for the file?
>Or are you looking in the same file that IDLE produced?
>I would expect the code to create a new file in the current
>directory - wherever you started the program. Did you look
>there?
>
>-----------------------
>while vDraws > 0:
>     LotNumbers = random.sample(range(1,45), 6) #random numbers from
>range
>into list)
>     strgOutput=",".join(str(i) for i in LotNumbers)
>#??????converting list
>to string to store it.
>     fout = open("draw__output.dat", "a")
>     fout.write(strgOutput + "\n")  #writing string to file
>     fout.close()
>--------------------------
>
>It's probably better to put the open/close outside the loop
>
>fout = open(...,'w')
>while vDraws > 0
>     ...
>     fout.write(strgOutput + '\n')
>     vDraws -= 1
>fout.close()
>
>That way you can use 'w' to write the file(unless you really want the
>previous runs to be in there too.) Also this will save the program
>opening and closing the file for each line which is quite a slow
>process.
>
>HTH,
>
>
>--
>Alan Gauld
>Author of the Learn to Program web site
>http://www.freenetpages.co.uk/hp/alan.gauld
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor

_________________________________________________________________
Windows Live Mail: Slim, Persoonlijk, Betrouwbaar en het blijft natuurlijk 
gratis! http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl



More information about the Tutor mailing list