How to modify this script?

K

Kurt Hansen

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

Chris Angelico

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
 
K

Kurt Hansen

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

Kurt Hansen

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

Chris Angelico

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

Kurt Hansen

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

Chris Angelico

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
 
K

Kurt Hansen

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

Subimal Deb

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
 
K

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. In the meantime I can just remove any unnecessary "colspan="1"
with a macro.

Thanks for your help.
 
K

Kurt Hansen

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
 
C

Chris Angelico

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
 
C

Chris Angelico

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
 
K

Kurt Hansen

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';
 
C

chaouche yacine

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.



________________________________
From: Kurt Hansen <[email protected]>
To: (e-mail address removed)
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
 
K

Kurt Hansen

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 <[email protected]>
*To:* (e-mail address removed)
*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
 
G

Gertjan Klein

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

Thomas Rachel

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
 
T

Thomas Rachel

Am 07.01.2013 18:56 schrieb Gertjan Klein:
(Watch out for line wraps! I don't know how to stop Thunderbird from
inserting them.)

Do "insert as quotation" (in German Thunderbird: "Als Zitat einfügen"),
or Strg-Shift-O. Then it gets inserted with a ">" before and in blue.

Just remove the > and the space after it; the "non-breaking property" is
kept.

Example:

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

without and

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

with this feature.

HTH,

Thomas
 
C

chaouche yacine

Well tell me how do you use this script in gedit, are you using it as a plugin ? are you putting this code somewhere ? I'll try to do the same on my side and try to understand how it works.






________________________________
From: Kurt Hansen <[email protected]>
To: (e-mail address removed)
Sent: Monday, January 7, 2013 5:42 PM
Subject: Re: Howto modify this script?

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

Thankyou for trying to help.
------------------------------------------------------------------------
*From:* Kurt Hansen <[email protected]>
*To:* (e-mail address removed)
*Sent:* Sunday, January 6, 2013 3:21PM
*Subject:* Re: How to modify this script?

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
-- http://mail.python.org/mailman/listinfo/python-list
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top