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 = '\n'; for line in lines: output += ''; columns = line.split("\t"); for item in columns: output += '' + item + ' ' output += '\n'; output += ''; 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 Green apple $1 5 Green apples $4 10 Green apples $7 Since there's only one "field" in the first line, I want this output: Price table - insted of Price table How to? Thank you i advance. -- Venlig hilsen Kurt Hansen -- http://mail.python.org/mailman/listinfo/python-list
Re: How to modify this script?
Den 06/01/13 13.52, Chris Angelico skrev: On Sun, Jan 6, 2013 at 11:42 PM, Kurt Hansen wrote: Since there's only one "field" in the first line, I want this output: Price table - insted of Price table 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 += '' + item + ' ' "innermost"? I have replaced this with yours, but all the marked text are deleted: for item in columns: output += '' + item + ' ' 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?
Den 06/01/13 13.58, chaouche yacine skrev: if len(columns) != 3: colspan = 3 - len(columns) + 1 output += '' % (colspan) + item + ' ' I did not test. Use with caution. I've tried to put it in several different places in the script, but with no luck; remember that I'm not experienced, so please tell me exactly where it's surposed to be inserted. Could you eventually show the complete modified script? -------- *From:* Kurt Hansen *To:* 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 = '\n'; for line in lines: output += ''; columns = line.split("\t"); for item in columns: output += '' + item + ' ' output += '\n'; output += ''; 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 Green apple $1 5 Green apples $4 10 Green apples $7 Since there's only one "field" in the first line, I want this output: Price table - insted of Price table 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?
Den 06/01/13 14.44, Chris Angelico wrote: On Mon, Jan 7, 2013 at 12:34 AM, Kurt Hansen 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 = '\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?
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 "%s\n" % line [cut] I surpose I shall put your code between $< and >? printed >>> Price table 1 Green apple $1 5 Green apples $4 10 Green apples $7 >>> Aha, so you tested it yourself? When running this in Gedit on four lines of tab-separated text the output is: %s\n" % line def addcolumn(item,nb_columns): if nb_columns != 3: return "%s" % (3 - nb_columns + 1, item) return "%s" % item output = "\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 += "" print output > -- Venlig hilsen Kurt Hansen -- http://mail.python.org/mailman/listinfo/python-list
Re: How to modify this script?
Den 06/01/13 15.20, Chris Angelico wrote: On Mon, Jan 7, 2013 at 1:03 AM, Kurt Hansen 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 += '' + item + ' ' 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?
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 = '\n'; for line in lines: output += ''; columns = line.split("\t"); if len(columns)==1: output += '', line, '' else: for item in columns: output += '' + item + ' ' output += '\n'; output += ''; 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?
Den 06/01/13 15.52, Chris Angelico skrev: On Mon, Jan 7, 2013 at 1:40 AM, Kurt Hansen wrote: failed: cannot concatenate 'str' and 'tuple' objects The problem is this line: output += '', line, '' Change it to: output += '' + line + '' :-) Something happened allright, but ... Output with this change: ' + line + '' else: for item in columns: output += '' + item + ' ' output += '\n'; output += ''; return output > -- Venlig hilsen Kurt Hansen -- http://mail.python.org/mailman/listinfo/python-list
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 *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 "%s\n" % line [cut] I surpose I shall put your code between $< and >? > printed > > >>> > Price table > 1 Green apple $1 > 5 Green apples $4 > 10 Green apples $7 > > >>> Aha, so you tested it yourself? When running this in Gedit on four lines of tab-separated text the output is: %s\n" % line def addcolumn(item,nb_columns): if nb_columns != 3: return "%s" % (3 - nb_columns + 1, item) return "%s" % item output = "\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 += "" 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?
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?
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 += '' + item + ' ' with if len(columns) >= 3: output += '' else: output += '' output += item + ' ' (untested as well; keep the indentation in mind!) Thanks, Thomas, but ... The script stops right bofore = 3: output += '' 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?
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?
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