How to modify this script?

chaouche yacine yacinechaouche at yahoo.com
Sun Jan 6 15:01:47 CET 2013



Well, I'm not answering your question since I am rewriting the script, because I prefer it this way :)

def addline(line):
    return "<tr>%s</tr>\n" % line

def addcolumn(item,nb_columns):
    if nb_columns != 3:
        return "<td colspan='%s'>%s</td>" % (3 - nb_columns + 1, item)
    return "<td>%s</td>" % item

output = "<table>\n"
for line in file("data.txt"):
    items = line.strip().split("\t")
    columns = ""
    for item in items :
        columns += addcolumn(item,len(items))
    output  += addline(columns)


output += "</table>"
print output


printed

>>> <table>
<tr><td colspan='3'>Price table</td></tr>
<tr><td>1 </td><td> Green apple </td><td> $1</td></tr>
<tr><td>5 </td><td> Green apples </td><td> $4</td></tr>
<tr><td>10 </td><td> Green apples </td><td> $7</td></tr>
</table>
>>> 




________________________________
From: Kurt Hansen <kurt at ugyldig.invalid>
To: python-list at python.org 
Sent: Sunday, January 6, 2013 2:38 PM
Subject: Re: How to modify this script?

Den 06/01/13 13.58, chaouche yacine skrev:
> if len(columns) != 3:
>     colspan = 3 - len(columns) + 1
>     output += '<td colspan=%s>' % (colspan) + item + '</td> '
>
> I did not test. Use with caution.

I've tried to put it in several different places in the script, but with 
no luck; remember that I'm not experienced, so please tell me exactly 
where it's surposed to be inserted. Could you eventually show the 
complete modified script?

>
> ------------------------------------------------------------------------
> *From:* Kurt Hansen <kurt at ugyldig.invalid>
> *To:* python-list at python.org
> *Sent:* Sunday, January 6, 2013 1:42 PM
> *Subject:* How to modify this script?
>
> http://www.tuxradar.com/content/save-time-gedit-snippets:
>
> To convert tab-separated text lines into a HTML-table:
>
> $<
> lines = $GEDIT_SELECTED_TEXT.split("\n");
> output = '<table\>\n';
>
> for line in lines:
>      output += '<tr\>';
>
>      columns = line.split("\t");
>      for item in columns:
>          output += '<td\>' + item + '</td\> '
>
>      output += '</tr\>\n';
>
> output += '</table\>';
> return output
>  >
>
> I would like to make a small modification (I'm not a programmer myself).
> Let's say I have these lines:
>
> Price table
> 1 <tab> Green apple <tab> $1
> 5 <tab> Green apples <tab> $4
> 10 <tab> Green apples <tab> $7
>
> Since there's only one "field" in the first line, I want this output:
>
> <tr><td colspan="3">Price table</td></tr>
>
> - insted of
>
> <tr><td>Price table</td></tr>
>
> How to? Thank you i advance.
> -- Venlig hilsen
> Kurt Hansen
> -- http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
Venlig hilsen
Kurt Hansen
-- 
http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130106/b518249a/attachment.html>


More information about the Python-list mailing list