[Tutor] help on raw_input()
Hi, i wanted to get a string from raw_input like this raw_input('') \n\nsomestring but the problem is raw input will return the string '\\n\\nsomestring' My question is Are there any function to convert back those string to '\n\nsomestring' ? Thanks - Ahhh...imagining that irresistible new car smell? Check outnew cars at Yahoo! Autos.___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
ammar azif [EMAIL PROTECTED] wrote i wanted to get a string from raw_input like this raw_input('') \n\nsomestring OK, Can you explain precisely what you want the string to contain. \n is the string representation of a newline. Do you want to enter something that starts with two newlines? Or do you literally want the sequence \,n,\,n? If its the latter thats what Python has stored. The double slash is only there when python displays the result (Try using len and comparing if you aren't convinced) If you want to actually capture newline characters from raw_input, thats more tricky. But before we get to that can you clarify what you actually want? Alan G. ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
Actually i wanted to write a http client using just he low level socket module. The program will prompt the user asking for input and the use will type http commands like 'GET /index.html HTTP/1.0\r\nHost: somehost\r\n\r\n' when i use the raw_input function , the string that i get is 'GET /index.html HTTP/1.0\\r\\nHost: somehost\\r\\n\\r\\n' is there any easy way other than modify this string ? Perhaps regular expression? Alan Gauld [EMAIL PROTECTED] wrote: ammar azif wrote i wanted to get a string from raw_input like this raw_input('') \n\nsomestring OK, Can you explain precisely what you want the string to contain. \n is the string representation of a newline. Do you want to enter something that starts with two newlines? Or do you literally want the sequence \,n,\,n? If its the latter thats what Python has stored. The double slash is only there when python displays the result (Try using len and comparing if you aren't convinced) If you want to actually capture newline characters from raw_input, thats more tricky. But before we get to that can you clarify what you actually want? Alan G. ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor - Ahhh...imagining that irresistible new car smell? Check outnew cars at Yahoo! Autos.___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
ammar azif wrote: Hi, i wanted to get a string from raw_input like this raw_input('') \n\nsomestring but the problem is raw input will return the string '\\n\\nsomestring' This is a bit confusing to talk about because the actual contents of the string differ from what is printed. I don't know if you realize that or not so I'll start at the beginning. In [3]: s= raw_input(' ') one\r\ntwo In [4]: s Out[4]: 'one\\r\\ntwo' When the interpreter outputs a string, it outputs it in the form of a string constant. Any actual \ in the string is escaped with an extra \. The string s contains single \ characters. You can verify this by getting the length: In [5]: len(s) Out[5]: 10 or by outputting it with print, which just outputs the actual characters: In [6]: print s one\r\ntwo So s does contain the same characters as typed. My question is Are there any function to convert back those string to '\n\nsomestring' ? You don't want the four literal characters \, r, \, n, you want a carriage return / newline combination. raw_input() doesn't interpret escape characters but there is a way to convert them: In [7]: t=s.decode('string_escape') Now t contains a carriage return / newline instead of the escape characters: In [8]: t Out[8]: 'one\r\ntwo' In [9]: len(t) Out[9]: 8 In [10]: print t one two Kent ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
ammar azif [EMAIL PROTECTED] wrote Actually i wanted to write a http client using just he low level socket module. I won;t ask why! But the httplib module probably does what you want just in case you dodn't realize it existed... The program will prompt the user asking for input and the use will type http commands like 'GET /index.html HTTP/1.0\r\nHost: somehost\r\n\r\n' OK, So you want the user to acrtually type \,n,\,n and you will then send that string to be interpreted as newlines? when i use the raw_input function , the string that i get is 'GET /index.html HTTP/1.0\\r\\nHost: somehost\\r\\n\\r\\n' The double \\ doesn''t actually exist its just Python telling you that it is a literal \ character not an escaped sequence. As I said earlier if you check the len() of the string it will only have one character per backslash. I think it's already doing what you want! You just need to turn the \n's that the user entered into newline characters, Kent has shown you how to do that with the decode() method... -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help on raw_input()
Thanks the encode method really helps. Alan Gauld [EMAIL PROTECTED] wrote: ammar azif wrote Actually i wanted to write a http client using just he low level socket module. I won;t ask why! But the httplib module probably does what you want just in case you dodn't realize it existed... The program will prompt the user asking for input and the use will type http commands like 'GET /index.html HTTP/1.0\r\nHost: somehost\r\n\r\n' OK, So you want the user to acrtually type \,n,\,n and you will then send that string to be interpreted as newlines? when i use the raw_input function , the string that i get is 'GET /index.html HTTP/1.0\\r\\nHost: somehost\\r\\n\\r\\n' The double \\ doesn''t actually exist its just Python telling you that it is a literal \ character not an escaped sequence. As I said earlier if you check the len() of the string it will only have one character per backslash. I think it's already doing what you want! You just need to turn the \n's that the user entered into newline characters, Kent has shown you how to do that with the decode() method... -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor - Ahhh...imagining that irresistible new car smell? Check outnew cars at Yahoo! Autos.___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor