以下脚本询问用户信息,将信息存储为变量,然后创建一个文件,将变量插入文本中的各个位置。创建的文件是一个html网页,然后可以在我的Web服务器上运行。该示例显示了4组变量并且运行良好。但是,我想询问用户所需的总数,然后执行一个循环,不断询问相同的信息(即视频编号,描述,文件夹名称),直到达到总数。对于每次迭代,我想生成html文本的选择部分。我是一个Python noob所以非常感谢任何帮助/反馈。提前谢谢!
# The next 12 variables prompt the use for the label, description and folder names for 4 videos
Video1_Num = raw_input('Enter the number label for Video 1: ')
Video1_Desc = raw_input('Enter the description of Video 1: ')
Video1_Fold = raw_input('Enter the name of the folder where video 1 is stored: ')
Video2_Num = raw_input('Enter the number label for Video 2: ')
Video2_Desc = raw_input('Enter the description of Video 2: ')
Video2_Fold = raw_input('Enter the name of the folder where video 2 is stored: ')
Video3_Num = raw_input('Enter the number label for Video 3: ')
Video3_Desc = raw_input('Enter the description of Video 3: ')
Video3_Fold = raw_input('Enter the name of the folder where video 3 is stored: ')
Video4_Num = raw_input('Enter the number label for Video 4: ')
Video4_Desc = raw_input('Enter the description of Video 4: ')
Video4_Fold = raw_input('Enter the name of the folder where video 4 is stored: ')
# The next line opens up a file with an html extension
f = open('index123.html','w')
# The following will be written into the file
html_page = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="jquery-1.4.4.js" type="text/javascript" charset="utf-8">
</script>
<script type="text/javascript">
$(document).ready(function() {
$("span.prj-slide1").click(function(){
$("#select_opt1").slideToggle('fast');
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$("span.prj-slide2").click(function(){
$("#select_opt2").slideToggle('fast');
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$("span.prj-slide3").click(function(){
$("#select_opt3").slideToggle('fast');
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$("span.prj-slide4").click(function(){
$("#select_opt4").slideToggle('fast');
});
});
</script>
<title>Demo</title>
</head>
<body>
<div id="header"><a href="index123.html"><img src="logo.png" width="390" height="60" border="0" /></a></div>
<div id="select_head">Select the video you wish to review:</div>
<div id="select_opt">
<p><span class="prj-slide1"><a href="" onclick="return false;"><b>Video %s:</b>  %s</a></span></p>
<div id="select_opt1" style="display: none;">
<video width="320" height="240" controls>
<source src="./%s/%s_1.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
<br></br>
<p><span class="prj-slide2"><a href="" onclick="return false;"><b>Video %s:</b>  %s</a></span></p>
<div id="select_opt2" style="display: none;">
<video width="320" height="240" controls>
<source src="./%s/%s_1.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
<br></br>
<p><span class="prj-slide3"><a href="" onclick="return false;"><b>Video %s:</b>  %s</a></span></p>
<div id="select_opt3" style="display: none;">
<video width="320" height="240" controls>
<source src="./%s/%s_1.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
<br></br>
<p><span class="prj-slide4"><a href="" onclick="return false;"><b>Video %s:</b>  %s</a></span></p>
<div id="select_opt4" style="display: none;">
<video width="320" height="240" controls>
<source src="./%s/%s_1.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
<br></br>
</div>
</body>
</html>""" %(Video1_Num, Video1_Desc, Video1_Fold, Video1_Fold, Video2_Num, Video2_Desc, Video2_Fold, Video2_Fold, Video3_Num, Video3_Desc, Video3_Fold, Video3_Fold, Video4_Num, Video4_Desc, Video4_Fold, Video4_Fold)
#'html_page gets written into the file and is closed. I then manually copy the newly created file to my web server.
f.write(html_page)
f.close()
答案 0 :(得分:0)
您可以使用dict列表。
video_list = []
假设你得到三个值,num_val,desc_str和fold_num作为输入
然后制作一个视频词典为video={'num': num_val,'desc':'desc_str','fold_num':fold_num}
现在,您可以继续按照下面的说明在video_list中推送它,并可以访问它们,如下所述。 Imp注意:在输入数据时,可能需要按照int或string等数据类型进行转换。
video_list =[]
Video_count =4
for n in range(Video_count):
num_val= input('enter number...')
desc_str= input('enter desc...')
fold_num= input('enter fold...')
video={'num': num_val,'desc':'desc_str','fold_num':fold_num}
video_list.append(video)
您可以按索引和元素访问元素,如下所示:
video_list[index]['num_val']
其中index是介于0和len之间的任何数字(video_list)-1(包括两者)。 num_val是dict的元素,它可以是num_val,desc_str和fold_num中的任何一个。