Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to modify this script?

Reply
Thread Tools

How to modify this script?

 
 
Kurt Hansen
Guest
Posts: n/a
 
      01-06-2013
http://www.tuxradar.com/content/save...edit-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
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      01-06-2013
On Sun, Jan 6, 2013 at 11:42 PM, Kurt Hansen <(E-Mail Removed)> wrote:
> 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.


It's actually quite simple, as long as you don't mind the junk of
colspan="1" on all the other cells. Just replace the innermost loop
with:

for item in columns:
output += '<td colspan="' + (4-len(columns)) + '"\>' +
item + '</td\> '

Untested, but it ought to work - as long as you never have _more_
cells in the line.

ChrisA
 
Reply With Quote
 
 
 
 
Kurt Hansen
Guest
Posts: n/a
 
      01-06-2013
Den 06/01/13 13.52, Chris Angelico skrev:
> On Sun, Jan 6, 2013 at 11:42 PM, Kurt Hansen <(E-Mail Removed)> wrote:
>> 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.


> It's actually quite simple, as long as you don't mind the junk of
> colspan="1" on all the other cells.


I do, but I would like to test anyway

Just replace the innermost loop
> with:
>
> for item in columns:
> output += '<td colspan="' + (4-len(columns)) + '"\>' +
> item + '</td\> '


"innermost"? I have replaced this with yours, but all the marked text
are deleted:

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

> Untested, but it ought to work - as long as you never have _more_
> cells in the line.

--
Regards
Kurt Hansen
 
Reply With Quote
 
Kurt Hansen
Guest
Posts: n/a
 
      01-06-2013
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 <(E-Mail Removed)>
> *To:* http://www.velocityreviews.com/forums/(E-Mail Removed)
> *Sent:* Sunday, January 6, 2013 1:42 PM
> *Subject:* How to modify this script?
>
> http://www.tuxradar.com/content/save...edit-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
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      01-06-2013
On Mon, Jan 7, 2013 at 12:34 AM, Kurt Hansen <(E-Mail Removed)> wrote:
> "innermost"? I have replaced this with yours, but all the marked text are
> deleted:


Here's the full code, with my change:

$<
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 colspan="' + (4-len(columns)) + '"\>' +
item + '</td\> '

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

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


It's only one line of code that needs to be changed. Python loops (and
other control structures) are defined by indentation, so the innermost
loop is the one that starts furthest to the right.

Chris Angelico
 
Reply With Quote
 
Kurt Hansen
Guest
Posts: n/a
 
      01-06-2013
Den 06/01/13 14.44, Chris Angelico wrote:
> On Mon, Jan 7, 2013 at 12:34 AM, Kurt Hansen <(E-Mail Removed)> wrote:
>> "innermost"? I have replaced this with yours, but all the marked text are
>> deleted:

>
> Here's the full code, with my change:
>
> $<
> lines = $GEDIT_SELECTED_TEXT.split("\n");
> output = '<table\>\n';


I'm sorry to bother you, Chris, but applying the snippet with your code
in Gedit still just deletes the marked, tab-separated text in the editor.
--
Venlig hilsen
Kurt Hansen
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      01-06-2013
On Mon, Jan 7, 2013 at 1:03 AM, Kurt Hansen <(E-Mail Removed)> wrote:
> I'm sorry to bother you, Chris, but applying the snippet with your code in
> Gedit still just deletes the marked, tab-separated text in the editor.


Ah, whoops. That would be because I had a bug in the code (that's why
I commented that it was untested). Sorry about that! Here's a fixed
version:

$<
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 colspan="' + str(4-len(columns)) +
'"\>' + item + '</td\> '

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

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



Note that it's a single line:

output += '<td colspan="' + str(4-len(columns)) + '"\>' + item + '</td\> '

If your newsreader (or my poster) wraps it, you'll need to unwrap that
line, otherwise you'll get an IndentError.

That version should work.

ChrisA
 
Reply With Quote
 
Kurt Hansen
Guest
Posts: n/a
 
      01-06-2013
Den 06/01/13 15.01, chaouche yacine wrote:
> 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

[cut]

I surpose I shall put your code between $< and >?

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


Aha, so you tested it yourself?

When running this in Gedit on four lines of tab-separated text the
output is:

%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
>

--
Venlig hilsen
Kurt Hansen
 
Reply With Quote
 
Subimal Deb
Guest
Posts: n/a
 
      01-06-2013
Kurt,
Try this:


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

for line in lines:
output += '<tr\>';

columns = line.split("\t");
if len(columns)==1:
output += '<tr\><td colspan="3"\>', line, '</td\></tr\>'
else:
for item in columns:
output += '<td\>' + item + '</td\> '

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

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


----------------
All I have done is to
If there is one item in the tab-separated line :
print the line as a row spanning 3 columns
else:
print the items in the line as an item in each column
----------------
I have not tried this snippet in Gedit - so use with caution.
----------------
good luck,
Subimal Deb




On Sunday, January 6, 2013 6:12:11 PM UTC+5:30, Kurt Hansen wrote:
> http://www.tuxradar.com/content/save...edit-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



 
Reply With Quote
 
Kurt Hansen
Guest
Posts: n/a
 
      01-06-2013
Den 06/01/13 15.20, Chris Angelico wrote:
> On Mon, Jan 7, 2013 at 1:03 AM, Kurt Hansen <(E-Mail Removed)> wrote:
>> I'm sorry to bother you, Chris, but applying the snippet with your code in
>> Gedit still just deletes the marked, tab-separated text in the editor.


> Ah, whoops. That would be because I had a bug in the code (that's why
> I commented that it was untested). Sorry about that! Here's a fixed
> version:
>

[cut]>
> Note that it's a single line:
>
> output += '<td colspan="' + str(4-len(columns)) + '"\>' + item + '</td\> '
>
> If your newsreader (or my poster) wraps it, you'll need to unwrap that
> line, otherwise you'll get an IndentError.


Ahhh, I did'nt realize that. Now it works

> That version should work.


It certainly does. I'll keep it and use it until at better solution is
found. In the meantime I can just remove any unnecessary "colspan="1"
with a macro.

Thanks for your help.
--
Venlig hilsen
Kurt Hansen
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to modify default Stored Procedure template in VS.NET database project ES ASP .Net 2 08-20-2004 08:19 PM
Regexp: modify $1 within the replace clause? Yaroslav Perl 2 04-21-2004 05:07 PM
Re: Error: can't modify scalar reference nobull@mail.com Perl 0 08-18-2003 04:56 PM
Re: Error: can't modify scalar reference Purl Gurl Perl 0 08-18-2003 07:51 AM



Advertisments