Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将值列表从python脚本逐个传递到html页面_Python - Fatal编程技术网

将值列表从python脚本逐个传递到html页面

将值列表从python脚本逐个传递到html页面,python,Python,我需要将一个包含五个值的列表从python脚本传递到html页面。我运行了一个循环,并将该值逐个传递到html页面。但它只在浏览器中打印最后一个值。我的html代码的格式为: <html> <head></head> <body> <p>{{value}}</p> </body> </html> 您好,我认为您需要做的是将列表元素连接到字符串 我假设您有以下列表: xlist = ["mangoes

我需要将一个包含五个值的列表从python脚本传递到html页面。我运行了一个循环,并将该值逐个传递到html页面。但它只在浏览器中打印最后一个值。我的html代码的格式为:

<html>
<head></head>
<body>
<p>{{value}}</p>
</body>
</html>

您好,我认为您需要做的是将列表元素连接到字符串

我假设您有以下列表:

xlist = ["mangoes","grapes","apples","oranges"]
xstr = ""
for id in xlist:
    xstr+='<tr><td>' + id + ' </td><td>' + getLabel(id) + '</td></tr>'
执行以下操作将得到一个字符串,其中包含分隔的列表元素:

xstr = ' '.join(xlist)
print(xstr)
芒果葡萄苹果橙子

如果您希望它们分别位于单独的p标记内,可以尝试以下操作:

xstr = ""
for e in xlist:
    xstr+='<p>'+e+'</p>'
print(xstr)
芒果

葡萄

苹果

橙子

现在您可以将字符串传递给html。希望这能解决你的问题

 This is the modified code .I need to print the output in browser in tabular format.
    def getLabel(i):

               db = MySQLdb.connect("localhost","root","","cpi" )
               cursor=db.cursor()
               cursor.execute('''select meaning from code where 
               code ='%d' ''' % (i))
               data=cursor.fetchall()
               return data
      xstr1 = ""
      for i in index:
            xstr1 +=str(getLabel(event[i]))+" "


      return render_template('output.html',value=xstr1) 


This is output.html
<html>
<head>
</head>

    <body>
        <table>

            {{value}}
        </table>
    </body>


</html>
根据评论中的讨论修改答案:

我假设这里的这些数字就像一个id,每个都有一个对应的唯一值。在这种情况下,您可以按如下方式对其进行修改:

xlist = ["mangoes","grapes","apples","oranges"]
xstr = ""
for id in xlist:
    xstr+='<tr><td>' + id + ' </td><td>' + getLabel(id) + '</td></tr>'
现在,您应该定义getLabel,以便它接受id作为参数,并从数据库中获取相应的标签并返回它

此外,您的html文件应如下所示:

<body>
    <table>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
        {{value}}<!--This will be a set of table rows -->
    </table>
</body>

请使用脚本文件更新您的问题。同时添加您的预期输出。您希望如何显示所有五个值?我也附上了我的python脚本。请检查并提出建议。@BishakhaKumari我相信每次调用render_模板时,您都会使用给定的值呈现整个页面。因此,通过在循环中调用它,可以一次又一次地呈现整个页面。必须将render_模板函数调用移到for循环之外。另外,您正在将一个列表传递到要打印在p标记内的列表中,我认为这不是您要寻找的。请尝试一下我的解决方案,看看它是否满足您的需要。谢谢,它工作正常,但如果我希望在浏览器中以表格格式打印所有值,我无法这样做。它正在一行打印。请提出一些建议。@BishakhaKumari您可以通过添加标记使它们显示在下一行中。xstr+=''+e+'

'。或者,如果您希望它是完全表格化的,您可以用标记替换p标记,并将变量包含在html文件中的标记中。如果您想要完整的代码解决方案,请分享您希望它的外观以及列表中的示例值。假设应用上述解决方案后,xstr中存储的值为:xstr=25 33 40 53 52。每个值都有一些有意义的字符串,我需要从数据库MySQL获取这些字符串,然后将与每个值相关的含义呈现到html页面中。i、 e.我需要以表格格式打印通过python脚本从数据库获取到浏览器的每个值的含义。我使用的是flask Frameworkef getLabeli:db=MySQLdb.connectlocalhost,root,,上下文智能提供程序cursor=db.cursor cursor。执行'select means from list where code='i''data=cursor.fetchall返回数据xstr=for索引中的id:xstr+=+strevent[id]+''+getLabelstrevent[id]+printxstr return render_template'output.html',value=xstr仍然我得到TypeError:必须是str,而不是tuple@BishakhaKumari您好,我相信变量数据是一种类似于列表的元组,因为它存储sql查询的输出,因为它可能有多行。请尝试打印数据[0]并相应地修改return语句以返回标签。请提交格式化的代码,这是一种混乱。此外,这个问题完全超出了您最初问题的范围,建议将此类问题作为新问题发布。因此,如果答案解决了您最初的问题,请将其标记为解决方案。如果您仍然需要帮助,请再次在注释中告诉我[0]打印了什么数据。@BishakhaKumari请参考:我的索引器:元组索引超出范围。每个类标签仅包含一个指定的含义。请确定,然后尝试打印数据而不是数据[0],并让我知道它输出的内容。