Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36235 > unrolled thread

How to modify this script?

Started byKurt Hansen <kurt@ugyldig.invalid>
First post2013-01-06 13:42 +0100
Last post2013-01-08 17:22 +0100
Articles 9 on this page of 29 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-06 13:42 +0100
    Re: How to modify this script? Chris Angelico <rosuav@gmail.com> - 2013-01-06 23:52 +1100
      Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-06 14:34 +0100
        Re: How to modify this script? Chris Angelico <rosuav@gmail.com> - 2013-01-07 00:44 +1100
          Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-06 15:03 +0100
            Re: How to modify this script? Chris Angelico <rosuav@gmail.com> - 2013-01-07 01:20 +1100
              Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-06 15:30 +0100
                Re: How to modify this script? Chris Angelico <rosuav@gmail.com> - 2013-01-07 01:41 +1100
                Re: How to modify this script? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2013-01-08 15:18 +0100
                  Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-09 10:21 +0100
    Re: How to modify this script? chaouche yacine <yacinechaouche@yahoo.com> - 2013-01-06 04:58 -0800
      Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-06 14:38 +0100
        Re: How to modify this script? chaouche yacine <yacinechaouche@yahoo.com> - 2013-01-06 06:01 -0800
          Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-06 15:21 +0100
            Re: How to modify this script? chaouche yacine <yacinechaouche@yahoo.com> - 2013-01-06 07:12 -0800
              Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-07 17:42 +0100
                Re: How to modify this script? chaouche yacine <yacinechaouche@yahoo.com> - 2013-01-08 07:31 -0800
                  Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-09 10:07 +0100
                    Re: How to modify this script? chaouche yacine <yacinechaouche@yahoo.com> - 2013-01-09 02:23 -0800
                      Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-09 12:04 +0100
                        Re: How to modify this script? chaouche yacine <yacinechaouche@yahoo.com> - 2013-01-09 10:25 -0800
                          Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-11 05:35 +0100
    Re: How to modify this script? Subimal Deb <subimal.deb@gmail.com> - 2013-01-06 06:22 -0800
      Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-06 15:40 +0100
        Re: How to modify this script? Chris Angelico <rosuav@gmail.com> - 2013-01-07 01:52 +1100
          Re: How to modify this script? Kurt Hansen <kurt@ugyldig.invalid> - 2013-01-06 16:05 +0100
    Re: How to modify this script? Gertjan Klein <gklein@xs4all.nl> - 2013-01-07 18:56 +0100
      Re: How to modify this script? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2013-01-08 15:22 +0100
        Re: How to modify this script? Gertjan Klein <gklein@xs4all.nl> - 2013-01-08 17:22 +0100

Page 2 of 2 — ← Prev page 1 [2]


#36514

Fromchaouche yacine <yacinechaouche@yahoo.com>
Date2013-01-09 10:25 -0800
Message-ID<mailman.333.1357756068.2939.python-list@python.org>
In reply to#36487

[Multipart message — attachments visible in raw view] — view raw

Indeed, the console shows a traceback where data is misinterpreted, maybe due to my triple protective quotes around $GEDIT_SELECTED_TEXT. Try without them, like so (it worked for me) : 

$<
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"
selected_text = $GEDIT_SELECTED_TEXT
for line in selected_text.split("\n"):
    items = line.strip().split("\t")
    columns = ""
    for item in items :
        columns += addcolumn(item,len(items))
    output  += addline(columns)

output += "</table\>"
return output>



________________________________
 From: Kurt Hansen <kurt@ugyldig.invalid>
To: python-list@python.org 
Sent: Wednesday, January 9, 2013 12:04 PM
Subject: Re: How to modify this script?
 
Den 09/01/13 11.23, chaouche yacine skrev:
> I figrued it out. Copy/paste exactly these lines in the snippets tool.
> You can bind it to a key as you may know, I bound it to Ctrl-E. So paste
> it in a new snippet (keep the original in a safe place), bind to a key,
> select the text you want to html-tableize and hit the key binding. In my
> case it worked.
> 
> $<
[cut]
> def addline(line):

Spooky behavior. Yes, the green-apple-example also works for me with your new script, BUT ...!

Try to copy the table content on this page:
http://www.danacord.dk/frmsets/records/732-r.html
which is a realistic scenario. That's whar I am doing these days.

Pasting it into Gedit and running the snippet blanks the edit area (on MY Mac at least).

And yes: I have pasted your code excatly and I've double-checked for linewraps. Everything is okay.

For your cenvenience I have put borders on the table online (see link above). You may ommit the rows after track 14. Not that it makes any differerence, but that block is surposed to be formatted differerent. I do that manually afterwards ... if not ... ;-)
-- Regards
Kurt Hansen
-- http://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#36599

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-11 05:35 +0100
Message-ID<50ef96a5$0$285$14726298@news.sunsite.dk>
In reply to#36514
Kurt wrote:
 >
 > Spooky behavior. Yes, the green-apple-example also works for me with
 > your new script, BUT ...!
 >
 > Try to copy the table content on this page:
 > http://www.danacord.dk/frmsets/records/732-r.html
 > which is a realistic scenario. That's whar I am doing these days.
 >
 > Pasting it into Gedit and running the snippet blanks the edit area (on
 > MY Mac at least).
 >
 > And yes: I have pasted your code excatly and I've double-checked for
 > linewraps. Everything is okay.

Chaouche replied:
>
> Indeed, the console shows a traceback where data is misinterpreted,
> maybe due to my triple protective quotes around $GEDIT_SELECTED_TEXT.
> Try without them, like so (it worked for me) :

Yes!!! :-)

Of course it would be nice if the script could be developed to take into 
account some of the antics I have made in my tables over the years, but 
with this script the basic codes of rows and columns is formatted 
properly; refinements of some collapsed fields with with line breaks 
a.o. is up to me now ;-).

Thanks to you and others who have participated in this thread.
-- 
Venlig hilsen
Kurt Hansen

[toc] | [prev] | [next] | [standalone]


#36255

FromSubimal Deb <subimal.deb@gmail.com>
Date2013-01-06 06:22 -0800
Message-ID<3b31ab41-f531-4c81-acf1-219d3c605935@googlegroups.com>
In reply to#36235
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-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

[toc] | [prev] | [next] | [standalone]


#36257

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-06 15:40 +0100
Message-ID<50e98ce2$0$294$14726298@news.sunsite.dk>
In reply to#36255
Den 06/01/13 15.22, Subimal Deb wrote:
> 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
-- 
Venlig hilsen
Kurt Hansen

[toc] | [prev] | [next] | [standalone]


#36259

FromChris Angelico <rosuav@gmail.com>
Date2013-01-07 01:52 +1100
Message-ID<mailman.172.1357483942.2939.python-list@python.org>
In reply to#36257
On Mon, Jan 7, 2013 at 1:40 AM, Kurt Hansen <kurt@ugyldig.invalid> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#36260

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-06 16:05 +0100
Message-ID<50e992c5$0$294$14726298@news.sunsite.dk>
In reply to#36259
Den 06/01/13 15.52, Chris Angelico skrev:
> On Mon, Jan 7, 2013 at 1:40 AM, Kurt Hansen <kurt@ugyldig.invalid> wrote:
>> 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>'

:-)

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

output += '</table\>';
return output
 >
-- 
Venlig hilsen
Kurt Hansen

[toc] | [prev] | [next] | [standalone]


#36367

FromGertjan Klein <gklein@xs4all.nl>
Date2013-01-07 18:56 +0100
Message-ID<50eb0c3e$0$6939$e4fe514c@news2.news.xs4all.nl>
In reply to#36235
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.

[toc] | [prev] | [next] | [standalone]


#36433

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2013-01-08 15:22 +0100
Message-ID<kcha2v$nst$1@r03.glglgl.gl>
In reply to#36367
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

[toc] | [prev] | [next] | [standalone]


#36438

FromGertjan Klein <gklein@xs4all.nl>
Date2013-01-08 17:22 +0100
Message-ID<50ec47d5$0$6942$e4fe514c@news2.news.xs4all.nl>
In reply to#36433
Thomas Rachel wrote:

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

Ah, I didn't think of that, thanks. Laborious but manageable. But:

> Example:

Both your examples are wrapped, in the same way. (I checked the message 
source, it's not some setting on my end.) It appears that, although 
Thunderbirds editor remembers the "don't wrap" setting, wrapping still 
occurs before sending. (The wrapping-before-sending code uses a slightly 
different algorithm than the editor too, so messages often look 
different after sending. Thunderbirds editor is really bad.)

A last attempt: two (equal) quotes, the second one with the leading "> " 
removed:

>           output += ('<td colspan=%s\>' % max_columns) + line + '</td\></tr\>\n'

           output += ('<td colspan=%s\>' % max_columns) + line + 
'</td\></tr\>\n'

I'm curious how this will come out.

Regards,
Gertjan.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web