开发者

Python: referencing lines in a table

开发者 https://www.devze.com 2023-01-24 06:20 出处:网络
I am trying to take lines from a file and put them into a table that will be displayed on the web. I need to be able to reference these lines individually to alter the table information using an if...

I am trying to take lines from a file and put them into a table that will be displayed on the web. I need to be able to reference these lines individually to alter the table information using an if...else statement.

Can anyone help me find a way to reference these lines when they are pulled through - this is my code so far.

#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>'


Set an id HTML attribute for each table entry (or row, depending on your needs). E.g.

<tr id="Foo">


Use format strings to your advantage. For instance, If I wanted to conditionally add a greeting, I would default the variable to the empty string and change it, based on my mood. Also:

  • Instead of instantiating and maintaining a counter, consider using enumerate().
  • Try to steer clear of escaping characters.
  • Maintain a clean consistent style (i.e. you had some html attributes using ', some using ", and one not using anything).

Example:

#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)

P.S. It's a little late, and I'm a bit tired, so I'm sorry if I can't spell, or I missed anything.

0

精彩评论

暂无评论...
验证码 换一张
取 消