Re: How to modify this script?

2013-01-10 Thread Kurt Hansen

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


Re: How to modify this script?

2013-01-09 Thread Kurt Hansen

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


Re: How to modify this script?

2013-01-09 Thread Kurt Hansen

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


Re: How to modify this script?

2013-01-09 Thread chaouche yacine
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-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-09 Thread Kurt Hansen

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


Re: How to modify this script?

2013-01-09 Thread chaouche yacine
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-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-08 Thread Thomas Rachel

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


Re: How to modify this script?

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


Re: How to modify this script?

2013-01-08 Thread 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 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
   trtd colspan='3'Price table/td/tr
   trtd1 /tdtd Green apple /tdtd $1/td/tr
   trtd5 /tdtd Green apples /tdtd $4/td/tr
   trtd10 /tdtd Green apples /tdtd $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-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-08 Thread Gertjan Klein

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.


--
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-07 Thread 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 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
  trtd colspan='3'Price table/td/tr
  trtd1 /tdtd Green apple /tdtd $1/td/tr
  trtd5 /tdtd Green apples /tdtd $4/td/tr
  trtd10 /tdtd Green apples /tdtd $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


Re: How to modify this script?

2013-01-07 Thread Gertjan Klein

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.


--
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-06 Thread Chris Angelico
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:

 trtd colspan=3Price table/td/tr

 - insted of

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


Re: How to modify this script?

2013-01-06 Thread chaouche yacine
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:

trtd colspan=3Price table/td/tr

- insted of

trtdPrice table/td/tr

How to? Thank you i advance.
-- Venlig hilsen
Kurt Hansen
-- http://mail.python.org/mailman/listinfo/python-list-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-06 Thread Kurt Hansen

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:

trtd colspan=3Price table/td/tr

- insted of

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


Re: How to modify this script?

2013-01-06 Thread 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?





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

trtd colspan=3Price table/td/tr

- insted of

trtdPrice 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


Re: How to modify this script?

2013-01-06 Thread Chris Angelico
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-06 Thread chaouche yacine


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
trtd colspan='3'Price table/td/tr
trtd1 /tdtd Green apple /tdtd $1/td/tr
trtd5 /tdtd Green apples /tdtd $4/td/tr
trtd10 /tdtd Green apples /tdtd $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:

 trtd colspan=3Price table/td/tr

 - insted of

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


Re: How to modify this script?

2013-01-06 Thread Kurt Hansen

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


Re: How to modify this script?

2013-01-06 Thread Chris Angelico
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-06 Thread Kurt Hansen

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
trtd colspan='3'Price table/td/tr
trtd1 /tdtd Green apple /tdtd $1/td/tr
trtd5 /tdtd Green apples /tdtd $4/td/tr
trtd10 /tdtd Green apples /tdtd $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


Re: How to modify this script?

2013-01-06 Thread 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';

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:
 
 
 
 trtd colspan=3Price table/td/tr
 
 
 
 - insted of
 
 
 
 trtdPrice table/td/tr
 
 
 
 How to? Thank you i advance.
 
 -- 
 
 Venlig hilsen
 
 Kurt Hansen


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

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


Thanks for your help.
--
Venlig hilsen
Kurt Hansen
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-06 Thread Chris Angelico
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-06 Thread Kurt Hansen

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


Re: How to modify this script?

2013-01-06 Thread Chris Angelico
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 += 'trtd colspan=3', line, '/td/tr'

Change it to:

output += 'trtd colspan=3' + line + '/td/tr'

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-06 Thread Kurt Hansen

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 += 'trtd colspan=3', line, '/td/tr'

Change it to:

output += 'trtd 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to modify this script?

2013-01-06 Thread chaouche yacine
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
 trtd colspan='3'Price table/td/tr
 trtd1 /tdtd Green apple /tdtd $1/td/tr
 trtd5 /tdtd Green apples /tdtd $4/td/tr
 trtd10 /tdtd Green apples /tdtd $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-- 
http://mail.python.org/mailman/listinfo/python-list