Kurt Hansen wrote:

To convert tab-separated text lines into a HTML-table:

As you apparently didn't receive answers that worked for you I tried to get what you want to work and test it in Gedit. Here's the result:

$<
lines = $GEDIT_SELECTED_TEXT.split("\n");
output = '<table\>\n';

max_columns = 0
for line in lines:
    col_count = len(line.split("\t"))
    if col_count \> max_columns:
        max_columns = col_count

for line in lines:
    if line == '':
        continue

    output += '<tr\>';

    columns = line.split("\t");
    if len(columns) == 1:
output += ('<td colspan=%s\>' % max_columns) + line + '</td\></tr\>\n'
        continue

    for item in columns:
        output += '<td\>' + item + '</td\>'

    output += '</tr\>\n';

output += '</table\>';
return output
>

(Watch out for line wraps! I don't know how to stop Thunderbird from inserting them.)

It really isn't all that difficult. The code determines the (maximum) number of columns present. It then processes each line; if one is found with exactly one column (i.e., no tabs), it applies a colspan equal to the maximum number of columns. This works for your test and similar data.

As I said, this is copy/pasted from a working Gedit snippet. If it works for you, I'd try experimenting a bit -- what should happen when the number of columns is larger than 1 but less than the maximum? Programming isn't magic. You might start enjoying it.

HTH,
Gertjan.


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to