Solved

Formatting Python output

Posted on 2014-09-12
7
275 Views
Last Modified: 2014-09-13
The following is a cgi page which has been written in Python3.4.  It is the action page of a html form. The pages does process the form input and does give an output which is correct content wise. However it is not formatted correctly and looks like this :

Helloo ('james', 'KANE', 'jamesdkane@gmail.com', '2014-09-09')!

I would like an output something like this :

Hello James Kane
jamesdkane@gmail.com
2014-09-09


If someone could take a look at the code below and fix the code and/or point me to some on line document it would be great !! Thanks for reading this.  jim

#!C:\Python34\python.exe
import cgi
def htmlTop():
      print("""Content-type:text/html\n\n
             <DOCTYPE html>
             <html lang="en">
                 <head>
                  <meta charset="utf-8"/>
                  <title> My Server-side template</title>
             </head>
             <body>""")

def htmlTail():
      print("""</body>
             </html>""")
      
def getData():
      formData = cgi.FieldStorage()
      firstname = formData.getvalue('firstname')
      secondname = formData.getvalue('secondname')
      youremail = formData.getvalue('youremail')
      birthday = formData.getvalue('birthday')
      return firstname,secondname,youremail,birthday
     
#main program

if __name__== "__main__":
      try:
            htmlTop()
            firstName = getData()
            print("Helloo {0}!".format(firstName))          
            htmlTail()
      except:
            cgi.print_exception()

Open in new window

0
Comment
Question by:jameskane
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40320336
Try replacing line 31 with:
print("Hello {0[0]} {0[1]}<br/>\n{0[2]}<br/>\n{0[3]}<br/>\n".format(firstName))

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40320338
Couple of extra remarks:
- I added code tags to the code in your question to be able to refer to the line number
- there seems to be an error in your code: <DOCTYPE html> shouldn't that be <!DOCTYPE html>
- I used <br/> instead of <br> because you have a meta tag using it as well, but probably both are unnecessary.
0
 

Author Comment

by:jameskane
ID: 40320563
Robert, very many thanks !!

Works perfectly.

Would you have a pointer to an online resource which deals with formatting topic ?

THANKS AGAIN !!
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Closing Comment

by:jameskane
ID: 40320564
Just Great !!!!
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40320584
Well the documentation is kind of complex but there are ample examples: https://docs.python.org/3.4/library/string.html#format-string-syntax
0
 

Author Comment

by:jameskane
ID: 40320609
thanks very much again !
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your app took Google’s lash recently, here are the 5 most likely reasons.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question