How to modify this script?

Kurt Hansen

Usenet Archive
Joined
Sep 7, 2004
Messages
14
Reaction score
0
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';

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.
 

Chris Angelico

Usenet Archive
Joined
Mar 28, 2011
Messages
4,720
Reaction score
0
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
 

chaouche yacine

Usenet Archive
Joined
Jan 5, 2013
Messages
16
Reaction score
0
if len(columns) != 3:
   colspan = 3 - len(columns) + 1
   output += '<td colspan=%s>' % (colspan) + item + '</td> '

I did not test. Use with caution.

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
 

Kurt Hansen

Usenet Archive
Joined
Sep 7, 2004
Messages
14
Reaction score
0
Den 06/01/13 13.52, Chris Angelico skrev:
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:
 

Kurt Hansen

Usenet Archive
Joined
Sep 7, 2004
Messages
14
Reaction score
0
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?
 

Chris Angelico

Usenet Archive
Joined
Mar 28, 2011
Messages
4,720
Reaction score
0
"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';

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
 

chaouche yacine

Usenet Archive
Joined
Jan 5, 2013
Messages
16
Reaction score
0
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>

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
*To:*
*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';
...
 

Kurt Hansen

Usenet Archive
Joined
Sep 7, 2004
Messages
14
Reaction score
0
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.
 

Chris Angelico

Usenet Archive
Joined
Mar 28, 2011
Messages
4,720
Reaction score
0
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';

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
 

Kurt Hansen

Usenet Archive
Joined
Sep 7, 2004
Messages
14
Reaction score
0
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 $ said:
printed

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

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)
 

Subimal Deb

Usenet Archive
Joined
Jan 6, 2013
Messages
1
Reaction score
0
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';

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
 

Kurt Hansen

Usenet Archive
Joined
Sep 7, 2004
Messages
14
Reaction score
0
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.
 

Kurt Hansen

Usenet Archive
Joined
Sep 7, 2004
Messages
14
Reaction score
0
Kurt,
Try this:
[cut]

I've tested it on my original example:

Price table
1 Green apple $1
5 Green apples $4
10 Green apples $7

With all four lines selected it makes an error. With only three (without the first line) it works all right.

The error message says:

Execution of the Python command (lines = 'Price table\n1\tGreen apple\t$1\n5\tGreen apples\t$4\n10\tGreen apples\t$7'.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) failed: cannot concatenate 'str' and 'tuple' objects
 

Chris Angelico

Usenet Archive
Joined
Mar 28, 2011
Messages
4,720
Reaction score
0
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.

Excellent! You'll find that Subimal's solution doesn't have those colspan="1" lines, so take your pick as to which way you want to go.

ChrisA
 

Chris Angelico

Usenet Archive
Joined
Mar 28, 2011
Messages
4,720
Reaction score
0
failed: cannot concatenate 'str' and 'tuple' objects

The problem is this line:

output += '<tr><td colspan="3">', line, '</td></tr>'

Change it to:

output += '<tr><td colspan="3">' + line + '</td></tr>'

ChrisA
 

Kurt Hansen

Usenet Archive
Joined
Sep 7, 2004
Messages
14
Reaction score
0
Den 06/01/13 15.52, Chris Angelico skrev:
The problem is this line:

output += '<tr><td colspan="3">', line, '</td></tr>'

Change it to:

output += '<tr><td colspan="3">' + line + '</td></tr>'

:)

Something happened allright, but ...
Output with this change:

<td colspan="3">' + line + '</td></tr>'
else:
for item in columns:
output += '<td\>' + item + '</td\> '

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

chaouche yacine

Usenet Archive
Joined
Jan 5, 2013
Messages
16
Reaction score
0
I'm not confident this would run on gedit. It works on a python interpreterif you have a file named data.txt in the same directory containing your sample data.

It surely has to do with how gedit works then, because the "$" sign isn't used in python, this business should be a gedit convention. And sorry, I can't help on that, I'm not a user of gedit myself. Fortunately others have answered and I beleive one of the solutions worked for you.

Sent: Sunday, January 6, 20133:21 PM
Subject: Re: How to modify this script?

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 $ said:
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 initems :
        columns += addcolumn(item,len(items))
    output  += addline(columns)

-- Venlig hilsen
Kurt Hansen
-- http://mail.python.org/mailman/listinfo/python-list
 

Kurt Hansen

Usenet Archive
Joined
Sep 7, 2004
Messages
14
Reaction score
0
Den 06/01/13 16.12, chaouche yacine skrev:
I'm not confident this would run on gedit. It works on a python interpreter if you have a file named data.txt in the same directory containing your sample data.

It surely has to do with how gedit works then, because the "$" sign isn't used in python, this business should be a gedit convention. And sorry, I can't help on that, I'm not a user of gedit myself. Fortunately others have answered and I beleive one of the solutions worked for you.

It does not seem to be the case :-(

Thank you for trying to help.
*From:* Kurt Hansen
*To:*
*Sent:* Sunday, January 6, 2013 3:21 PM
*Subject:* Re: How to modify this script?

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 $ said:
printed

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

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)

-- Venlig hilsen
Kurt Hansen
-- http://mail.python.org/mailman/listinfo/python-list
 

Gertjan Klein

Usenet Archive
Joined
May 2, 2004
Messages
24
Reaction score
0
Kurt said:
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';

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

Thomas Rachel

Usenet Archive
Joined
Oct 20, 2010
Messages
197
Reaction score
0
Am 06.01.2013 15:30 schrieb Kurt Hansen:
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.

That would be simple:

Replace

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

with

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

(untested as well; keep the indentation in mind!)

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,051
Messages
3,104,251
Members
248,135
Latest member
tobystanx

Latest Threads

Top