Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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
Web2py Python根据数据库结果中的true/false分割行_Python_For Loop_Split_Web2py - Fatal编程技术网

Web2py Python根据数据库结果中的true/false分割行

Web2py Python根据数据库结果中的true/false分割行,python,for-loop,split,web2py,Python,For Loop,Split,Web2py,我在web2py/python中有以下代码: {{for row in db(db.t_problems.id==db.t_problems(request.args(0))).select(): }} 它抓住了所有的行,就像我需要的那样。这些行是实际的python结果,当我打印出来时,它们是: >>> testFunction(2, 3, 4) True >>> testFunction(2, 1, 4) False >>> legalT

我在web2py/python中有以下代码:

{{for row in db(db.t_problems.id==db.t_problems(request.args(0))).select(): }}
它抓住了所有的行,就像我需要的那样。这些行是实际的python结果,当我打印出来时,它们是:

>>> testFunction(2, 3, 4) True >>> testFunction(2, 1, 4) False >>> legalTriangles(-1, -1, -1) False
当您进行原始输出时,得到的结果如下:

>>> testFunction(2, 3, 4)\r\nTrue >>> testFunction(2, 1, 4)\r\nFalse >>> legalTriangles(-1, -1, -1)\r\nFalse
我需要做的是删除>>>,并在一个变量结果中使用testFunctionX、Y、Z,在另一个变量结果中使用True/False。我认为这可能会起作用,但循环只会剥离\r\n,而不会将它们放入新的变量中:

ios = row.f_tests.split('>>>') #results are now the testFunctions without the >>>
for io in ios:
     i = io.split("\r\n") 
结果是:

testFunction(2, 3, 4)True testFunction(2, 1, 4)False testFunction(-1, -1, -1)False
但我需要的是

func1 = testFunction(2, 3, 4)
res1 = True
func2 = testFunction(2, 1, 4)
res2 = False    

这样我就可以把它们放在桌子上了。有什么想法吗?谢谢大家!

在原始python中

s = ">>> testFunction(2, 3, 4)\r\nTrue >>> testFunction(2, 1, 4)\r\nFalse >>> legalTriangles(-1, -1, -1)\r\nFalse"
for func, result in [i.splitlines() for i in s.split(">>>") if i]:
    print "<tr><td>%s</td><td>%s</td></tr>" % (func.strip(), result.strip())
结果:

<tr><td>testFunction(2, 3, 4)</td><td>True</td></tr>
<tr><td>testFunction(2, 1, 4)</td><td>False</td></tr>
<tr><td>legalTriangles(-1, -1, -1)</td><td>False</td></tr>

您应该能够将其转换为web2py的模板或视图语言。我看到它支持for循环。

注意,您应该在控制器中进行db选择,并将结果传递给视图。我会的,但仍在学习web2py,因此我首先将实际代码放在页面中,然后在正确的情况下,将其移动到控制器。不过,谢谢你!这正是我需要的,谢谢!我不知道为什么我以前不能得到它,但你的代码运行得很好。出于某种原因,我总是遇到Python循环的问题,而不是其他语言的问题。