在python中创建一个表

时间:2015-03-21 19:48:53

标签: python html list tuples

我要求用户通过一系列问题回答他们,然后答案将被写入用户桌面上名为mydoc.doc的文件中。我现在遇到的问题是在Python中创建一个表,该表创建的行数与用户输入的访谈数相同。因此,如果用户输入5个面试,则会询问他们5次面试的开始/结束时间,面试类型和面试官姓名(每个"面试"存储在列表中的元组中)。然后,它需要在一行上输出每个元组,但是元组中的每个项目都在不同的列中输出。我开始通过在代码中放置HTML标记并构建一个类似的表来开始玩,但是我如何告诉程序执行5行并在另一列中输入元组中的每一个东西。这就是我到目前为止所拥有的......

import sys
outfile = open( r'/Users/x/Desktop/myDoc.txt', 'w' )
external = raw_input('is this an exernal invite: ')
inhouse = raw_input('is this an in house invite: ')
if external == 'y'and inhouse== 'y':
    cfname= raw_input('What is the Candidte First Name:')
    clname= raw_input('What is the Candidte Last Name:')
    interviewmonth= raw_input('What is the Month of the interview: ')
    interviewday= raw_input('What is the Day of the interview: ')
    cleadfname= raw_input('What is the recrutiers  first name of the interview: ')
    cleadlname= raw_input('What is the recruiters last name of the interview: ')
    i = 0
    lis = []
    n = int(raw_input("How many interviews are there? "))
    while n:
       i += 1
       istart = raw_input("Interview Start Time: ")
       iend= raw_input("Interview End Time: ")
       ipeople= raw_input("What are the interviewer names: ")
       itype= raw_input("What is the interview type: ")
       lis.append((istart, iend, ipeople, itype))
       n-=1
    a = '<html><head></head><body> Hi %s, </br> The interview is scheduled for <strong>%s %sth</strong>\
         <br/>if you have an questions please contact %s\
         <TABLE border=1>\
         <TR>\
         </TR>\
         <TR>\
         <TH>Interviewer</TH>\
         <TH>Interview Type</TH>\
         </TR>\
         <TR ALIGN="CENTER">\
         <TD>Data 1</TD>\
         <TD>Data 2</TD>\
         </TR>\
         </TABLE>\
         </body></html>'% (cfname, interviewmonth, interviewday, cleadfname)
    outfile.write(a)
outfile.close()

1 个答案:

答案 0 :(得分:0)

简单回答,使用循环。

首先,我建议用i + = 1和n - = 1和while(n)代替跳舞 对于范围(n)中的i:

接下来,一旦你使用所有元组建立了良好的lis,就可以循环遍历它。

这是一个例子,松散地基于你的,但没有我不得不思考并输入值。我在你的代码中查询了几件事,我在下面的这篇文章中有评论询问它们。

#! /usr/bin/python

cfname = "Victim"
interviewmonth = "Januember"
interviewday = "Fooday"
cleadfname = "The Boss"

n = 10
lis = []
for i in range(10):
  istart  = "istart  value %d"%i
  iend    = "iend    value %d"%i
  ipeople = "ipeople value %d"%i
  itype   = "itype   value %d"%i
  lis.append((istart, iend, ipeople, itype))

html = """
<html>
  <head></head>
  <body>
    <!-- Should have a p tag here -->
    Hi %s, 
    </br>The interview is scheduled for <strong>%s %sth</strong>
    <br/>if you have an questions please contact %s
    <!-- Should have a /p tag here -->
"""
print html%(cfname, interviewmonth, interviewday, cleadfname);

html = """
    <TABLE border=1>
      <TR> <!-- Why'd you have this blank line? -->
      </TR>
      <TR>
        <!-- Didn't understand your headings, should it not be the
             things in lis? 
        -->
        <TH>Start</TH>
        <th>End</th>
        <th>People</th>
        <TH>Interview Type</TH>
      </TR>
"""
print html

# Now here's where I think you want a loop
for interview in lis:
  html = """
      <TR ALIGN="CENTER">
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
      </TR>
  """
  print html % interview

html = """
    </TABLE>
  </body>
</html>
"""
print html