web2python基于true/false从数据库resu拆分行

2024-10-02 22:36:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

{{for row in db(db.t_problems.id==db.t_problems(request.args(0))).select(): }}

它占据了所有的行,就像我需要的那样。这些行是实际的python结果,当我打印出来时是:

^{pr2}$

(当你做一个原始输出,这就是你得到的:)

>>> testFunction(2, 3, 4)\r\nTrue >>> testFunction(2, 1, 4)\r\nFalse >>> legalTriangles(-1, -1, -1)\r\nFalse

我需要做的是删除>;>>;,并在一个变量结果中使用testFunction(X,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    

所以我可以把它们放在桌子上。有什么想法吗?谢谢您!在


Tags: the代码iniofalsetruefordb
1条回答
网友
1楼 · 发布于 2024-10-02 22:36:03

在原始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())

结果:

^{pr2}$

你应该能够把它翻译成web2py的模板或“视图”语言。我看到from here它支持for循环。在

相关问题 更多 >