import sys
outfile = open( r'/Users/x/Desktop/myDoc.txt', 'w' )
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><TABLE border=1><TR> </TR> <TR>\
<TH>Start</TH>\
<th>End</th>\
<th>People</th>\
<TH>Interview Type</TH></TR><TR ALIGN=CENTER></TR> \
<td>fff</td>dd<td>dddd</td><td>ddd</td><td>ddddd</td></TABLE></body></html>"
outfile.write(a)
outfile.close()
所以基本上这个print语句写入我的计算机上的文件,但是我遇到了在这个print语句中包含另一个循环的问题,因为如果用户说有5个访谈我需要5行,其中每行是一个元组在列表中,每列都是该元组中的每个项目(如果用户说6次采访,我需要6行,依此类推)。有没有办法做到这一点?
答案 0 :(得分:1)
这可以解决您的问题吗?循环只需要创建行,不应该将文档的开头和结尾放在循环中。
import sys
outfile = open( r'/Users/x/Desktop/myDoc.txt', 'w' )
i = 0
lis = []
n = int(raw_input("How many interviews are there? "))
table = "<html><head></head><body><TABLE border=1><TR>\
<TH>Start</TH>\
<th>End</th>\
<th>People</th>\
<TH>Interview Type</TH></TR><TR ALIGN=CENTER></TR> \
%s</TABLE></body></html>"
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("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" % (istart, iend, ipeople, itype))
n-=1
outfile.write(table % ''.join(lis))
outfile.close()
而不是&#34;而n&#34;你可以使用范围(n),那么你就不需要任何计数器变量。