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 20 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 1 of 2  [1] 2  Next page →


#36235 — How to modify this script?

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-06 13:42 +0100
SubjectHow to modify this script?
Message-ID<50e97123$0$294$14726298@news.sunsite.dk>
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] | [next] | [standalone]


#36238

FromChris Angelico <rosuav@gmail.com>
Date2013-01-06 23:52 +1100
Message-ID<mailman.160.1357476725.2939.python-list@python.org>
In reply to#36235
On Sun, Jan 6, 2013 at 11:42 PM, Kurt Hansen <kurt@ugyldig.invalid> 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

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


#36245

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-06 14:34 +0100
Message-ID<50e97d82$0$294$14726298@news.sunsite.dk>
In reply to#36238
Den 06/01/13 13.52, Chris Angelico skrev:
> On Sun, Jan 6, 2013 at 11:42 PM, Kurt Hansen <kurt@ugyldig.invalid> 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

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


#36248

FromChris Angelico <rosuav@gmail.com>
Date2013-01-07 00:44 +1100
Message-ID<mailman.167.1357479855.2939.python-list@python.org>
In reply to#36245
On Mon, Jan 7, 2013 at 12:34 AM, Kurt Hansen <kurt@ugyldig.invalid> 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

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


#36250

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-06 15:03 +0100
Message-ID<50e9844f$0$294$14726298@news.sunsite.dk>
In reply to#36248
Den 06/01/13 14.44, Chris Angelico wrote:
> On Mon, Jan 7, 2013 at 12:34 AM, Kurt Hansen <kurt@ugyldig.invalid> 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

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


#36252

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

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


#36256

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-06 15:30 +0100
Message-ID<50e98a77$0$294$14726298@news.sunsite.dk>
In reply to#36252
Den 06/01/13 15.20, Chris Angelico wrote:
> On Mon, Jan 7, 2013 at 1:03 AM, Kurt Hansen <kurt@ugyldig.invalid> 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

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


#36258

FromChris Angelico <rosuav@gmail.com>
Date2013-01-07 01:41 +1100
Message-ID<mailman.171.1357483295.2939.python-list@python.org>
In reply to#36256
On Mon, Jan 7, 2013 at 1:30 AM, Kurt Hansen <kurt@ugyldig.invalid> wrote:
> Den 06/01/13 15.20, Chris Angelico wrote:
>
>> On Mon, Jan 7, 2013 at 1:03 AM, Kurt Hansen <kurt@ugyldig.invalid> 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.

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

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


#36432

FromThomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Date2013-01-08 15:18 +0100
Message-ID<kch9so$nmo$1@r03.glglgl.gl>
In reply to#36256
Am 06.01.2013 15:30 schrieb Kurt Hansen:
> Den 06/01/13 15.20, Chris Angelico wrote:
>> On Mon, Jan 7, 2013 at 1:03 AM, Kurt Hansen <kurt@ugyldig.invalid> 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.

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

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


#36481

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-09 10:21 +0100
Message-ID<50ed3684$0$284$14726298@news.sunsite.dk>
In reply to#36432
>> Den 06/01/13 15.20, Chris Angelico wrote:
>
>>> That version should work.

> Am 06.01.2013 15:30 schrieb Kurt Hansen:
>
>> It certainly does. I'll keep it and use it until at better solution is
>> found.

On 08/01/13 15.18, Thomas Rachel wrote:
 >
> 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!)

Thanks, Thomas, but ...

The script stops right bofore
= 3:
     output += '<td\>'

This, and the rest of the script code is put out as the result, not my test.
-- 
Venlig hilsen
Kurt Hansen

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


#36239

Fromchaouche yacine <yacinechaouche@yahoo.com>
Date2013-01-06 04:58 -0800
Message-ID<mailman.161.1357477260.2939.python-list@python.org>
In reply to#36235

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

if len(columns) != 3:
   colspan = 3 - len(columns) + 1
   output += '<td colspan=%s>' % (colspan) + item + '</td> '

I did not test. Use with caution.




________________________________
 From: Kurt Hansen <kurt@ugyldig.invalid>
To: python-list@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

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


#36246

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-06 14:38 +0100
Message-ID<50e97e4c$0$294$14726298@news.sunsite.dk>
In reply to#36239
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@ugyldig.invalid>
> *To:* python-list@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

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


#36251

Fromchaouche yacine <yacinechaouche@yahoo.com>
Date2013-01-06 06:01 -0800
Message-ID<mailman.168.1357481051.2939.python-list@python.org>
In reply to#36246

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


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@ugyldig.invalid>
To: python-list@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@ugyldig.invalid>
> *To:* python-list@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

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


#36253

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-06 15:21 +0100
Message-ID<50e9885e$0$294$14726298@news.sunsite.dk>
In reply to#36251
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

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


#36262

Fromchaouche yacine <yacinechaouche@yahoo.com>
Date2013-01-06 07:12 -0800
Message-ID<mailman.173.1357485172.2939.python-list@python.org>
In reply to#36253

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

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.



________________________________
 From: Kurt Hansen <kurt@ugyldig.invalid>
To: python-list@python.org 
Sent: Sunday, January 6, 2013 3:21 PM
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

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


#36363

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-07 17:42 +0100
Message-ID<50eafb00$0$287$14726298@news.sunsite.dk>
In reply to#36262
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 <kurt@ugyldig.invalid>
> *To:* python-list@python.org
> *Sent:* Sunday, January 6, 2013 3:21 PM
> *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
>
>


-- 
Venlig hilsen
Kurt Hansen

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


#36436

Fromchaouche yacine <yacinechaouche@yahoo.com>
Date2013-01-08 07:31 -0800
Message-ID<mailman.280.1357659111.2939.python-list@python.org>
In reply to#36363

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

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 <kurt@ugyldig.invalid>
To: python-list@python.org 
Sent: Monday, January 7, 2013 5:42 PM
Subject: Re: How to 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 :-(

Thank you for trying to help.

> ------------------------------------------------------------------------
> *From:* Kurt Hansen <kurt@ugyldig.invalid>
> *To:* python-list@python.org
> *Sent:* Sunday, January 6, 2013 3:21 PM
> *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
>
>


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

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


#36480

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-09 10:07 +0100
Message-ID<50ed336f$0$284$14726298@news.sunsite.dk>
In reply to#36436
Den 08/01/13 16.31, chaouche yacine skrev:
> Well tell me how do you use this script in gedit, are you using it as a
> plugin ?

"Snippets" is a plugin, yes. It's included in the .app for Mac (v. 
2.30.2), but not activated af default.

Open "Tools" in the menu line and click "Manage snippets...". Here you 
can organize, add and edit snippets of texts. The feature olså has the 
ability to work with Python code inside the snippet content.

I am re-building a 15 years old homepage. The HTML code is handmade over 
the years and very varying, buggy etc., så I would like to renew the 
HTML for the table structure in an easy way.

Example: On this page: http://www.danacord.dk/frmsets/records/732-r.html 
I mark the content of the CD, copy it to the clipboard and paste it into 
the editing area in Gedit. cmd-a marks it all again and then I "run" the 
snippet upon the text, either using my self-defined hotkey or by pushing 
ctrl+space and select my snippet from a list.

The copied text is inserted as clean text without any HTML. The 
Python-snippet we are discussing recognizes tabs to separate the columns 
and adds the apprpriate HTML-code to it.
-- 
Regards
Kurt Hansen

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


#36484

Fromchaouche yacine <yacinechaouche@yahoo.com>
Date2013-01-09 02:23 -0800
Message-ID<mailman.310.1357726998.2939.python-list@python.org>
In reply to#36480

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

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.

$<
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 """$GEDIT_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>


Here's a screenshit, sorry screenshot :) http://h.dropcanvas.com/521xc/gedit.png


The python support in gedit snippets is very poor when it comes to debugging because there are traceback printed in the console, that means gedit actually breaks without even noticing the user about what went wrong (ex. : your snippet is malformed or has errors). I had to debug it using pdb.set_trace directly inside its source code to figure out what was wrong in the snippet.

If this doesn't work for you, please let me know.





________________________________
 From: Kurt Hansen <kurt@ugyldig.invalid>
To: python-list@python.org 
Sent: Wednesday, January 9, 2013 10:07 AM
Subject: Re: How to modify this script?
 
Den 08/01/13 16.31, chaouche yacine skrev:
> Well tell me how do you use this script in gedit, are you using it as a
> plugin ?

"Snippets" is a plugin, yes. It's included in the .app for Mac (v. 2.30.2), but not activated af default.

Open "Tools" in the menu line and click "Manage snippets...". Here you can organize, add and edit snippets of texts. The feature olså has the ability to work with Python code inside the snippet content.

I am re-building a 15 years old homepage. The HTML code is handmade over the years and very varying, buggy etc., så I would like to renew the HTML for the table structure in an easy way.

Example: On this page: http://www.danacord.dk/frmsets/records/732-r.html I mark the content of the CD, copy it to the clipboard and paste it into the editing area in Gedit. cmd-a marks it all again and then I "run" the snippet upon the text, either using my self-defined hotkey or by pushing ctrl+space and select my snippet from a list.

The copied text is inserted as clean text without any HTML. The Python-snippet we are discussing recognizes tabs to separate the columns and adds the apprpriate HTML-code to it.
-- Regards
Kurt Hansen
-- http://mail.python.org/mailman/listinfo/python-list

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


#36487

FromKurt Hansen <kurt@ugyldig.invalid>
Date2013-01-09 12:04 +0100
Message-ID<50ed4eaf$0$284$14726298@news.sunsite.dk>
In reply to#36484
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

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


Page 1 of 2  [1] 2  Next page →

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


csiph-web