[Tutor] 答复: Tutor Digest, Vol 97, Issue 72
Hello, I'm new to studying python, Seems you don't have to import the argument module. You can simply open the two files and write then to another file. By using file(".xx",r) can open a file with read, by using file("",w) can open a file with write. And then you can using xx.readline() to read a line from file, then xx.writeline() to write into another file. Hope this helps you. Thanks Xianming -邮件原件- 发件人: tutor-bounces+xianming.yan=intercallapac@python.org [mailto:tutor-bounces+xianming.yan=intercallapac@python.org] 代表 tutor-requ...@python.org 发送时间: 2012年3月28日 18:00 收件人: tutor@python.org 主题: Tutor Digest, Vol 97, Issue 72 Send Tutor mailing list submissions to tutor@python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to tutor-requ...@python.org You can reach the person managing the list at tutor-ow...@python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: 1. Re: I/O operation on closed file? (bob gailer) -- Message: 1 Date: Tue, 27 Mar 2012 10:16:36 -0400 From: bob gailer To: thao nguyen Cc: tutor@python.org Subject: Re: [Tutor] I/O operation on closed file? Message-ID: <4f71cbc4.7080...@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed in addition to posting the relevant code: please in future use a meaningful subject which I have provided this time. remember to reply-all so a copy goes to the list. On 3/26/2012 9:08 PM, thao nguyen wrote: > > Dear Support Team, > > I have built a function (enclosed here) to merge many files (in this > example is 2 files: "a1.txt" and "a2.txt") lines by lines. The output > file is called "final_file". However, i could not have it run > successfully. > > Content of "a1.txt": > 1 > 3 > 5 > > > Content of "a2.txt": > 2 > 4 > 6 > > > Content of "final_file.txt" will be like: > 1 > 2 > 3 > 4 > 5 > 6 > > > In Python, i called just written module: > > import argument > reload(argument) > argument.test(2,"C:/a1.txt","C:/a2.txt") > > and get the error as below: > "ValueError: I/O operation on closed file > File "c:\append.py", line 5, in > argument.test(2,"C:/a1.txt","C:/a2.txt") > File "c:\argument.py", line 28, in test > for line_data in f:" > > Could you please advise the resolution for this? > What does " I/O operation on closed file" suggest to you? -- Bob Gailer 919-636-4239 Chapel Hill NC -- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor End of Tutor Digest, Vol 97, Issue 72 * ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Strange ways of opening a file
On 03/28/2012 06:17 AM, Yan, Xianming wrote: > Hello, > Without a meaningful title, it's unclear just what your question is. When you do a reply on the digest, delete all the parts that aren't relevant to your post, and change the subject line to something appropriate. > I'm new to studying python, > > Seems you don't have to import the argument module. > The standard library has no module called argument. If you want to import a module, you need to write it, and save it to a file with a .py extension. Or perhaps you've installed such a module from some internet site. > You can simply open the two files and write then to another file. By using > file(".xx",r) can open a file with read, by using file("",w) can open > a file with write. > The standard way to open a file is with the built-in open() function. Otherwise your syntax is missing the quotes around the r and w. To open a text file, use: infile = open("", "r") outfile = open("", "w") > And then you can using xx.readline() to read a line from file, then > xx.writeline() to write into another file. > Again, assuming the data is text. > Hope this helps you. > > Thanks > Xianming -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 答复: Any book to study Python
> I'm reading your book and just typeing the TCP server and client example > scripts to my python and run it, but I did not get the expected results. > > For tstclnt.py(example 16.2,page 473 on the book),when I typed in vi and > execut it by python tstclnt.py, I get the error of 'No route to host'. > > Of course I modified the IP address to 192.168.1.11 which is on another linux > server in the script, I pinged the 192.168.1.11 and get successfully.(On > 192.168.1.11 I run the TCP server program, it is the one on the book) > > Please see the screen shot for detail. > > Can you give me some suggestion on this error? hi xianming, any of the tutors here can help you, not just me! :-) instead of a screenshot (many people use only terminals to read email), it's best to just cut-n-paste the code and error traceback into your email. based on what you sent in, it would be worthwhile for you to also post the server code and output as well. note that a positive ping and the client speaking to an address are somewhat but not entirely related. the reason is that different network protocols are being used. ping uses ICMP -- http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol -- while the code uses TCP -- http://en.wikipedia.org/wiki/Transmission_Control_Protocol -- instead. once you provide the server code and output, people may be able to help you better. best regards, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "A computer never does what you want... only what you tell it." wesley chun : wescpy at gmail : @wescpy/+wescpy Python training & consulting : CyberwebConsulting.com "Core Python" books : CorePython.com Python-flavored blog: wescpy.blogspot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Any book to study Python
On 03/28/2012 02:40 PM, wesley chun wrote: hi xianming, any of the tutors here can help you, not just me! :-) once you provide the server code and output, people may be able to help you better. best regards, -- wesley Agreed. It depends on firewalls between the two IP addresses. Also, the Linux server may have IP tables installed which might prevent access to that port but allow ping. Leam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Lists and While Loops
So I have to write a program that reads a message from the user and prints a new message that contains all the words from the original message but in the same order without repeating any of them unless they show up more than once in the original message. What I have thus far looks like this: message = input("Your message:") myList = message.split() y = random.choice(myList) z = len(myList) while z !=0: myList.remove(y) print(y) I can't figure out 1) How to remove "y" from the list and continue the loop; when I use .remove etc. it runs once then give me the error that "y" is not in the list. I imagine the answer is quite simple I'm just getting really frustrated trying to get this done any advice is appreciated. Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Any book to study Python
> > > > any of the tutors here can help you, not just me! :-) > > > > > once you provide the server code and output, people may be able to > > help you better. > Agreed. It depends on firewalls between the two IP addresses. Also, the > Linux server may have IP tables installed which might prevent access to > that port but allow ping. Or (if I remember correctly) allow access to the port but not ping. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists and While Loops
> I can't figure out > 1) How to remove "y" from the list and continue the loop; when I use .remove > etc. it runs once then give me the error that "y" is not in the list. > > I imagine the answer is quite simple I'm just getting really frustrated > trying to get this done any advice is appreciated. This should tell you why you run into your error: >>> message = "This is a message." >>> m_list = message.split() >>> m_list ['This', 'is', 'a', 'message.'] >>> y = random.choice(m_list) >>> y 'message.' >>> m_list.remove(y) >>> m_list ['This', 'is', 'a'] >>> y 'message.' You should probably move your y = random.choice() into the while loop. A simpler way than to remove items from a list is to use random.sample: >>> message = "This is a message." >>> m_list = message.split() >>> random.sample(m_list, len(m_list)) ['is', 'This', 'a', 'message.'] -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists and While Loops
On 3/28/2012 11:53 AM Ricky Brown said... So I have to write a program that reads a message from the user and prints a new message ok -- that contains all the words from the original message but in the same order without repeating any of them unless they show up more than once in the original message. ?? doesn't this mean repeat the exact same message? IOW if the user suppies: This is is a test test the above rule would resuire you to output exactly the same input I think What I have thus far looks like this: message = input("Your message:") so, "print message" does it... ... but I suspect your requirement isn't accurate. Emile myList = message.split() y = random.choice(myList) z = len(myList) while z !=0: myList.remove(y) print(y) I can't figure out 1) How to remove "y" from the list and continue the loop; when I use .remove etc. it runs once then give me the error that "y" is not in the list. I imagine the answer is quite simple I'm just getting really frustrated trying to get this done any advice is appreciated. Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists and While Loops
On Wed, Mar 28, 2012 at 2:53 PM, Ricky Brown wrote: > So I have to write a program that reads a message from the user and prints > a new message that contains all the words from the original message but in > the same order without repeating any of them unless they show up more than > once in the original message. > > What I have thus far looks like this: > > message = input("Your message:") > myList = message.split() > y = random.choice(myList) > z = len(myList) > while z !=0: > myList.remove(y) > print(y) > > I can't figure out > 1) How to remove "y" from the list and continue the loop; when I use > .remove etc. it runs once then give me the error that "y" is not in the > list. > > I imagine the answer is quite simple I'm just getting really frustrated > trying to get this done any advice is appreciated. > > Thanks > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > You have two main issues, one, you are not designing the program to do what is specified in the directions, and two, the program you did write has a few things that you need to work on. For the later portion, I've provided some commentary below in the body of your code that I have copied over. message = input("Your message:") myList = message.split() y = random.choice(myList) #here you have saved some random choice from your list to the variable y. z = len(myList) #Here you save the length of the list to the variable z. while z !=0: #the above says, keep doing this loop while z is not equal to 0. myList.remove(y) #Here you remove an item from the list, the item stored to the variable y from above. #it's important to note that this does not change the value of z and this does not change the value of y. print(y) #y gets printed once The loop then goes back up and since z has not changed (and in fact never will change, because you have never told it to change) the while loop runs through another iteration. However, once myList.remove(y) is evaluated again for the second time it throws an error because y is no longer in the list. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists and While Loops
On 28/03/2012 21:15, Emile van Sebille wrote: On 3/28/2012 11:53 AM Ricky Brown said... So I have to write a program that reads a message from the user and prints a new message ok -- that contains all the words from the original message but in the same order without repeating any of them unless they show up more than once in the original message. ?? doesn't this mean repeat the exact same message? IOW if the user suppies: This is is a test test the above rule would resuire you to output exactly the same input I think What I have thus far looks like this: message = input("Your message:") so, "print message" does it... ... but I suspect your requirement isn't accurate. Emile What the OP is asking for would output your original This is is a test test as is is test test I have a strong sense of Déjà vu, namely gathering requirements being far more difficult than writing code :) I've no idea whether the requirement given is right or wrong. Whatever the case I'd be using the built-in set or on older Pythons the sets module. myList = message.split() y = random.choice(myList) z = len(myList) while z !=0: myList.remove(y) print(y) I can't figure out 1) How to remove "y" from the list and continue the loop; when I use .remove etc. it runs once then give me the error that "y" is not in the list. I imagine the answer is quite simple I'm just getting really frustrated trying to get this done any advice is appreciated. Thanks -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lists and While Loops
Thank you all for your input it was extremely helpful. Robert you were spot on with what I was trying to do, I tried to implement the random.sample earlier but I couldn't see to get it too work -- running smoothly now. Sorry for the trouble :) On Wed, Mar 28, 2012 at 4:14 PM, Robert Sjoblom wrote: > > I can't figure out > > 1) How to remove "y" from the list and continue the loop; when I use > .remove > > etc. it runs once then give me the error that "y" is not in the list. > > > > I imagine the answer is quite simple I'm just getting really frustrated > > trying to get this done any advice is appreciated. > > This should tell you why you run into your error: > > >>> message = "This is a message." > >>> m_list = message.split() > >>> m_list > ['This', 'is', 'a', 'message.'] > >>> y = random.choice(m_list) > >>> y > 'message.' > >>> m_list.remove(y) > >>> m_list > ['This', 'is', 'a'] > >>> y > 'message.' > > You should probably move your y = random.choice() into the while loop. > > A simpler way than to remove items from a list is to use random.sample: > >>> message = "This is a message." > >>> m_list = message.split() > >>> random.sample(m_list, len(m_list)) > ['is', 'This', 'a', 'message.'] > > -- > best regards, > Robert S. > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor