[Tutor] Inserting special characters into urlencoded string

spir denis.spir at free.fr
Tue Feb 10 18:48:29 CET 2009


Le Tue, 10 Feb 2009 18:08:26 +0100,
pa yo <payo2000 at gmail.com> a écrit :

> Novice programmer here.
> 
> I am using urllib.urlencode to post content to a web page:
> 
> ...
> >>Title = "First Steps"
> >>Text = "Hello World."
> >>Username = "Payo2000"
> >>Content = Text + "From: " + Username
> >>SubmitText = urllib.urlencode(dict(Action = 'submit', Headline = Title, Textbox = Content))
> >>Submit = opener.open('http://www.website.com/index.php?', SubmitText)
> 
> This works fine to produce in this:
> 
> "Hello World From Payo2000"
> 
> ...on the page.
> 
> However I can't work out how to add a linefeed (urlencode: %0A) in
> front of Username so that I get:
> 
> "Hello World.
> From: Payo2000"
> 
> Any hints, tips or suggestions welcome!

In python (and many other languages) some special characters that are not easy to represent have a code:
LF : \n
CR : \r
TAB : \t
So just add \n to to first line to add an eol: Text = "Hello World.\n" [Beware that inside python \n actually both means char #10 and 'newline', transparently, whatever the os uses as 'newline' code for text files. Very handy.]
Alternatively, as you seem to be used to web codes, you can have hexa representation \x0a or even octal \012

Denis
-----
la vida e estranya


More information about the Tutor mailing list