Re: [Tutor] Newbie question: join() string method
> strgOutput=",".join(str(i) for i in LotNumbers) #??converting list > to string to store it. note that here, you are *not* converting the list to a string to store it. you are converting each list member to a string and creating a new "list" (actually generator [expression]) for the string.join() method to takes its contents (now strings) and concatenate them together into one large string delimited by ','s. cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie question: join() string method
"Moedeloos Overste" <[EMAIL PROTECTED]> 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@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie question: join() string method
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" <[EMAIL PROTECTED]> >Reply-To: "Andreas Kostyrka" <[EMAIL PROTECTED]> >To: "Moedeloos Overste" <[EMAIL PROTECTED]> >CC: tutor@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" <[EMAIL PROTECTED]> >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@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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie question: join() string method
Hi, I am not sure what 'random' does (what package is it from?), but list=['1','2','3'] slist=','.join(list) works, while list=[1,2,3] slist=','.join(list) does not. It appears 'join' only works on lists (and maybe tuples?) of string objects and the list must be passed in as an argument. Try translating your list into a list of string representation of numbers and then using join. Hope this is a useful clue, -Jason On 11/27/06, Moedeloos Overste <[EMAIL PROTECTED]> wrote: 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@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie question: join() string method
",".join(str(x) for x in intList) Andreas _ Ursprüngliche Mitteilung _ Betreff:[Tutor] Newbie question: join() string method Autor: "Moedeloos Overste" <[EMAIL PROTECTED]> 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@python.org http://mail.python.org/mailman/listinfo/tutor___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor