2010-11-11 50 views
2

我想从文件中取出行并将它们放入将显示在Web上的表中。我需要能够单独引用这些行来使用if ... else语句更改表信息。Python:在表中引用行

任何人都可以帮助我找到一种方法来引用这些行时,他们被拉 - 这是我的代码到目前为止。

#for each line in emaildomains - print out on page to view 
print '<form method=\'post\' name="updateUsers">' 
print '<table border="1">' 
print '<tr>' 
print '<th>Email Address</th>' 
print '<th>Delete Email</th>' 
print '<th>Make Changes?</th>' 
print '</tr>' 
n=1 
for line in emaildomains: 
    print '<tr>' 
    print '<td><input type="text" name=\"useraddress\", n, value ="%s">' %line 
    print '<input type="hidden" name=useraddress_org value ="%s"></td>' %line 
    print '<td><input type=\"radio\" name=\"deleteRadio\", n, style=margin-left:50px></td>' 
    print '<td><input type="submit" value="Edit Users" /></td>' 
    print '</tr>' 
    n+=1 
print '</table>' 
print '</form>' 

回答

2

设置每个表项中的id HTML属性(或行,根据您的需要)。例如。

<tr id="Foo"> 
0

使用格式字符串对您有利。例如,如果我想有条件地添加一个问候语,我会根据自己的心情将该变量默认为空字符串并对其进行更改。另外:

  • 而不是实例化和维护计数器,请考虑使用枚举()。
  • 试着避开逃跑的角色。
  • 保持一个清洁的一贯作风(即你有一些HTML属性使用”,有的用”,和一个不使用任何东西)

例:

#for each line in emaildomains - print out on page to view 
table_fs = """ 
<form method="post" name="updateUsers"> 
%s 
<table border="1"> 
<tr> 
<th>Email Address</th> 
<th>Delete Email</th> 
<th>Make Changes?</th> 
</tr> 
%s 
</table> 
</form> 
""" 

line_fs = """ 
<td> 
    %s 
    <input type="text" name="useraddress" %s value ="%s"> 
    <input type="hidden" name="useraddress_org" value ="%s"> 
</td> 
<td><input type="radio" name="deleteRadio", n, style=margin-left:50px></td> 
<td><input type="submit" value="Edit Users" /></td> 
""" 

good_mood = '' 
if i_have_cookies: 
    good_mood = '<h1>I LOVE COOKIES!</h1>' 

lines = [] 
for n, line in enumerate(emaildomains, 1): 
    greeting = '' 
    if i_like_this_persion: 
     greeting = 'Hi!' 
    line = [] 
    line.append(line_fs%(greeting, n, line, line)) 
    cells_string = '\n'.join(['<td>%s</td>'%x for x in line]) 
    row_string = '<tr>%s</tr>'%(cells_string) 
    lines.append(row_string) 

rows_string = '\n'.join(lines) 
print table_fs%(good_mood, rows_string) 

PS这是一个有点晚了,我有点累,所以我很抱歉,如果我不能拼写,或者我错过了任何东西。