'print' in a CGI app.

Discussion in 'Python' started by Andrew Chalk, Aug 3, 2003.

  1. Andrew Chalk

    Andrew Chalk Guest

    In a Python 2.2 app. running under CGI the statements

    print "Hello\n"
    print "World"

    print both words on the same line in IE6. How do I print the second one on a
    new line (i.e. respect the \n in the first statement).

    Many thanks!
     
    Andrew Chalk, Aug 3, 2003
    #1
    1. Advertising

  2. > print "Hello\n"
    > print "World"


    Try

    print "Hello<br>\n"
    print "World"

    or try

    print "<pre>"
    print "Hello\n"
    print "World"
    print "</pre>"

    --

    Dennis Reinhardt

    http://www.spamai.com?ng_py
     
    Dennis Reinhardt, Aug 3, 2003
    #2
    1. Advertising

  3. Andrew Chalk wrote:

    > In a Python 2.2 app. running under CGI the statements
    >
    > print "Hello\n"
    > print "World"
    >
    > print both words on the same line in IE6. How do I print the second one on a
    > new line (i.e. respect the \n in the first statement).


    Umm. This has nothing to do with Python... but hey :)
    Unless your CGI script is set to content-type: text/plain,
    you're supposed to print valid HTML.
    You'll have more success if you print the following:

    print "<html><body>" # begin HTML
    print "<pre>" # HTML 'preformatted' block
    print "Hello\n"
    print "World"
    print "</pre>" # end 'preformatted block'
    print "</body></html>" # end HTML


    --Irmen
     
    Irmen de Jong, Aug 3, 2003
    #3
  4. Andrew Chalk

    Andrew Chalk Guest

    Thanks to you, Timo, Dennis and Irmen. The <BR> trick was what I was using
    but I thought I must be missing something.

    Regards
    "Timo Virkkala" <> wrote in message
    news:TVcXa.4487$...
    > Andrew Chalk wrote:
    > > In a Python 2.2 app. running under CGI the statements
    > >
    > > print "Hello\n"
    > > print "World"
    > >
    > > print both words on the same line in IE6. How do I print the second one

    on a
    > > new line (i.e. respect the \n in the first statement).

    >
    > How about (untested):
    >
    > print "Hello<BR>\n"
    > print "World"
    >
    > ...since, AFAIK, it should be HTML that CGI's output...?
    >
    > --
    > Timo Virkkala |
    >
    > "In the battle between you and the world, bet on the world."
    >
     
    Andrew Chalk, Aug 3, 2003
    #4
  5. Andrew Chalk

    Patrick Lioi Guest

    "Andrew Chalk" <> wrote in message news:<ERcXa.1902$>...
    > In a Python 2.2 app. running under CGI the statements
    >
    > print "Hello\n"
    > print "World"
    >
    > print both words on the same line in IE6. How do I print the second one on a
    > new line (i.e. respect the \n in the first statement).
    >
    > Many thanks!


    Run the app, then go to "View"->"Source" in the browser. $10 says
    they are on separate lines. All browsers are expecting HTML output
    from CGI scripts. HTML ignores whitespace. The <br> tag comes in
    handy for forcing an endline:

    print "Hello<br>"
    print "World"
     
    Patrick Lioi, Aug 3, 2003
    #5
  6. "Andrew Chalk" <> wrote:

    > print "Hello\n"
    > print "World"


    > print both words on the same line in IE6.


    This is because IE is treating the page as HTML by default, where
    whitespace is not significant. If this is not what you want, set the
    'Content-Type' HTTP header to something else, eg.:

    #!/usr/bin/python

    print 'Content-Type: text/plain; charset=utf-8'
    print

    print 'Hello'
    print 'World'

    --
    Andrew Clover
    mailto:
    http://www.doxdesk.com/
     
    Andrew Clover, Aug 3, 2003
    #6
  7. Andrew Chalk

    dan Guest

    This is a random guess, but try:

    print "Hello<br>"
    print "World"

    "Andrew Chalk" <> wrote in message news:<ERcXa.1902$>...
    > In a Python 2.2 app. running under CGI the statements
    >
    > print "Hello\n"
    > print "World"
    >
    > print both words on the same line in IE6. How do I print the second one on a
    > new line (i.e. respect the \n in the first statement).
    >
    > Many thanks!
     
    dan, Aug 4, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jürgen Exner

    Re: CGI Perl "use CGI" statement fail

    Jürgen Exner, Jul 31, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,436
    Jürgen Exner
    Jul 31, 2003
  2. Shailan
    Replies:
    2
    Views:
    1,033
    Shailan
    Dec 15, 2003
  3. keto
    Replies:
    0
    Views:
    968
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    364
    David Cournapeau
    Dec 30, 2008
  5. learnerplates
    Replies:
    8
    Views:
    432
    John Bokma
    Jun 8, 2005
Loading...

Share This Page