python cgi表格问题~

python cgi表格问题~

我在python cgi里从调mysql里调数据的时候, 得出来是一个结果一个表格,例: http://www.ug.cs.usyd.edu.au/~ja ... cgi?q=&word=dog


如何把那些表格变为一个? 像这样:http://www.ug.cs.usyd.edu.au/~jali1814/assignment3/question2.html



这是我的一部分code:   

r = conn.cursor()
r.execute("select synset_id, word from wnsynset where synset_id in(select synset_id from wnsynset where word='%s') and word !='%s'" % (word, word))
q=r.fetchall()
print "<p class='a'>%d rows were returned</p>" % r.rowcount
for rows in r:


      print "<table border>"
      print "<tr><th>Synset_id</th><th>Synonyms</th></tr>"
      print "<tr><td>%s</td><td>%s</td></tr>" % (rows[0],rows[1])

      print "</table>"


先谢谢了

print "<table border>"
for rows in r:

      print "<tr><th>Synset_id</th><th>Synonyms</th></tr>"
      print "<tr><td>%s</td><td>%s</td></tr>" % (rows[0],rows[1])

print "</table>"


QUOTE:
原帖由 luffy.deng 于 2008-5-18 19:01 发表
print ""
for rows in r:

      print "Synset_idSynonyms"
      print "%s%s" % (rows[0],rows[1])

print ""

谢谢不过这样虽然变成了一个表格,但我想做的是所有synset_id相同的都放在一行里面, 请看下这个例子:
http://www.ug.cs.usyd.edu.au/~jali1814/assignment3/question2.html

表格应该是这样的:


SYNSET ID                        SYNONYMS
                           

2064081                       domestic dog, Canis familiaris

2685221                       andiron, firedog, dog-iron

7571296                       frank, frankfurter, hotdog, wiener
print "<table border>"
print "<tr><th>Synset_id</th><th>Synonyms</th></tr>"
for rows in r:
      print "<tr><td>%s</td><td>%s</td></tr>" % (rows[0],rows[1])
print "</table>"


QUOTE:
原帖由 luffy.deng 于 2008-5-18 22:19 发表
print ""
print "Synset_idSynonyms"
for rows in r:
      print "%s%s" % (rows[0],rows[1])
print ""

这回出来的是这样的:   有相同 synset_id 的 synonyms 并没有在一行里呀~

Synset_id   Synonyms

2064081   Canis_familiaris
2064081   domestic_dog
2685221   andiron
2685221   dog-iron
2685221   firedog
楼主你的代码有安全漏洞
可以判断一下前后两行的row[0],相同则连接row[1],或者在sql 里实现。另外关于安全漏洞请楼上明示。
列一段模拟的代码。如果结果集是按照Synset_id排序的,usingSequence();如果Synset_id没有顺序,usingDict()。

[Copy to clipboard] [ - ]
CODE:
import string

SynonymsData = [ ("2064081", "Canis_familiaris"), ("2064081", "domestic_dog"), ("2685221", "andiron"),
                ("2685221", "dog-iron"), ("2685221", "firedog"), ("3857710", "click"), ("3857710", "detent"),
                ("3857710", "pawl"), ("7571296", "frank"), ("7571296", "frankfurter"), ("7571296", "hotdog"),
                ("7571296", "hot_dog"), ("7571296", "weenie"), ("7571296", "wiener"), ("7571296", "wienerwurst"),
                ("9740502", "blackguard"), ("9740502", "bounder"), ("9740502", "cad"), ("9740502", "heel"),
                ("9740502", "hound"), ("9963790", "frump") ]

def usingDict (SynonymsData):
    SynonymsDict = {}
    for Synset_id, Synonyms in SynonymsData:
        if SynonymsDict.has_key( Synset_id ):
            SynonymsDict[ Synset_id ].append( Synonyms )
        else:
            SynonymsDict[ Synset_id ] = [Synonyms]

    print "<table border = \"1\">"
    print "<tr><th>Synset_id</th><th>Synonyms</th></tr>"
    for Synset_id, Synonyms in SynonymsDict.iteritems():
        print "<tr><td>%s</td><td>%s</td></tr>" % ( Synset_id, string.join(Synonyms, ', ') )
    print "</table>"

def usingSequence (SynonymsData):
    lastId = -1
    SynonymsList = []

    for Synset_id, Synonyms in SynonymsData:
        if Synset_id == lastId:
            SynonymsList.append( Synonyms )
        else:
            if len(SynonymsList) > 0:
                print "<tr><td>%s</td><td>%s</td></tr>" % ( lastId, string.join(SynonymsList, ', ') )
            else:
                print "<table border = \"1\">"
                print "<tr><th>Synset_id</th><th>Synonyms</th></tr>"

            lastId = Synset_id
            SynonymsList = [Synonyms]

    if len(SynonymsList) > 0:
        print "<tr><td>%s</td><td>%s</td></tr>" % ( lastId, string.join(SynonymsList, ', ') )
        print "</table>"

usingSequence( SynonymsData )

usingDict( SynonymsData )

SydneyUni的?

如果按row[0]排好序了,下面可以勉强工作

print "<table border>"
print "<tr><th>Synset_id</th><th>Synonyms</th></tr>"
rows=r.fetchall()
s=rows[0][1]

for i in range(1,len(rows)):

    if rows[0] == rows[i-1][0]:
        s=s+','+rows[1]
    else:
        print "<tr><td>%s</td><td>%s</td></tr>" % (rows[i-1][0],s)
        s=rows[1]
        if i == len(rows)-1:
            print "<tr><td>%s</td><td>%s</td></tr>" % (rows[0],rows[1])
print "</table>"
写错了一个地方
else:
........
   s=rows[1]