[Tutor] Newbie question: join() string method
Moedeloos Overste
kloosterjunkie at hotmail.com
Mon Nov 27 16:30:01 CET 2006
Ha!
Thanx for all the input everybody. I finally got it to work. The contents of
the list is now stored in the file without the []. The solution is in the
code below. Next step in my learning process is reading the file contents
and storing it in a dictionary.
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?
code:
vDraws = input("How many times do you want to draw the lottery? :>")
# Draw lottery numbers & writing them to file
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()
vDraws = vDraws - 1
>From: "Andreas Kostyrka" <andreas at kostyrka.org>
>Reply-To: "Andreas Kostyrka" <andreas at kostyrka.org>
>To: "Moedeloos Overste" <kloosterjunkie at hotmail.com>
>CC: tutor at python.org
>Subject: Re: [Tutor] Newbie question: join() string method
>Date: Mon, 27 Nov 2006 13:32:59 +0100
>
>",".join(str(x) for x in intList)
>
>Andreas
>
>_____ Ursprüngliche Mitteilung _____
>Betreff: [Tutor] Newbie question: join() string method
>Autor: "Moedeloos Overste" <kloosterjunkie at hotmail.com>
>Datum: 27. November 2006 13:20:41
>
>Hi,
>
>I'm trying to use the join() method on a list to be able to store the
>contents of that list in a file. Basically I want the contents of the list
>stored without the []. I've tried numerous ways of using the join() method,
>but they all return errors. I've tried using the tutorial and documentation
>on this one but still can't work it out.
>
>errors: TypeError: sequence item 0: expected string, int found
>
>
>
>code:
>LotNumbers = random.sample(range(1,45), 6) #random numbers from range into
>list)
> ','.join() #?what's
>the
>correct syntax?
> fout = open("draw__output.dat", "a")
> fout.write(LotNumbers) #writing string to
>file
> fout.close()
> vDraws = vDraws - 1
>
>Can someone show me the correct way to use the join() method in this case?
>
>Many thanks,
>
>Robert
>
>_________________________________________________________________
>De makers van Hotmail hebben groot nieuws! Meer weten? Klik hier!
>http://imagine-windowslive.com/mail/launch/default.aspx?Locale=nl-nl
>
>_______________________________________________
>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