Editing tabular data

rusi rustompmody at gmail.com
Tue Sep 3 12:58:18 EDT 2013


On Friday, August 2, 2013 12:05:53 PM UTC+5:30, Ben Finney wrote:
> Skip Montanaro writes:
> 
> > I really love Emacs, however... […]
> >
> > This is clearly a case where choosing the proper tool is important. I
> > agree that using a spreadsheet to edit a 3x5 CSV file is likely
> > overkill (might just as well use Notepad or TextEdit), but tabular
> > data are tabular data, no matter how they might be delimited, and if
> > there are many of those little data critters, there are better tools
> > than a text editor (or Python IDE) for maintaining them.
> 
> It seems an obvious thing for powerful text editors like Emacs and Vim
> to have a third-party mode for editing CSV data with a tabular
> interface.
> 
> 
> Indeed, such modes exist; one that I found immediately for Emacs is
> <URL:http://www.emacswiki.org/emacs/csv-mode.el>. Has anyone got a good
> Emacs mode for editing CSV data as a table and saving it back to CSV
> data?


Emacs users can have the cake and eat it too; ie use spreadsheet
functionality without having to use a separate spreadsheet file
and software.

The basic idea is to use org-mode which has a table
editor with spreadsheet functionality while continuing to live within a 
plain text editor.

It allows to edit a table entirely written in plain text in a visually appealing and clean way, while keeping a (less readable) python data
structure in sync with it.

The example file is below the ---------------

Caveats:
The orig_table string is there only make the source for the table.
The name orig_table is not necessary; a naked triple-string will also work.
The triple string is there to pacify python in the face of non valid syntax.
Ideal would have been comments but python does not have multiline comments

The table between the #BEGIN RECEIVE ORGTBL marks
and the #END RECEIVE ORGTBL marks
is the target or the recipient for the transformed version of the
plain text table.

Experiment as follows:
1. Save the stuff below --------- as something.py
2. Start editing the file in emacs

3. Join the 3 lines into 1 line with single space separators.
 +ORGTBL: SEND marks orgtbl-to-generic
  :lfmt "  \"%s\": [%s,%s,%s,%s,%s],"
  :llfmt "  \"%s\": [%s,%s,%s,%s,%s]"

   [It has to be one line, but if I kept it one line, it
   will be randomly be garbled in the mail!]

 This line gives the table a name ("marks") so that you can use
 several tables in one file, and it specifies how the syntax should
 be changed when syncing the python version of  the table data.

4. Start orgtbl minor mode with M-x orgtbl-mode
   Mode line should show python and orgtbl

5. Delete the contents (keep the 2 # lines intact) of the python table

6. Place cursor within the orig_table and 'send' it as follows

7. Send is done with any one of 'C-c C-c' or 'C-u C-c C-c' or 'C-u C-u C-c C-c'
   The first just sends the table as is
   The second recomputes the formulas top-down and then sends
   The third recomputes until fixpoint (you really should not be making such a  
   table!!)

8. Play with the table editor by using TAB and S-TAB to
   walk through fields and change them, use C-u C-c C-c again to
   sync the python version of the table

9. In case the above does now work (if your orgmode is too old)
   the orig_table_2 should hopefully work even for older org versions
   It furthermore shows the ability to skip columns and to format
   column widths to convenience.

----------------------------------
orig_table = """

#+ORGTBL: SEND marks orgtbl-to-generic
:lfmt "  \"%s\": [%s,%s,%s,%s,%s],"
:llfmt "  \"%s\": [%s,%s,%s,%s,%s]"
| abe    |  1 |  2 |  3 |  4 |    10 |
| beth   |  9 |  1 |  5 |  9 |    24 |
| catherine |  5 |  6 |  7 |  5 |    23 |
#+TBLFM: $6=$2+$3+$4+$5
"""

stud_db = {
# Dont handedit
# BEGIN RECEIVE ORGTBL marks
  "abe": [1,2,3,4,10],
  "beth": [9,1,5,9,24],
  "catherine": [5,6,7,5,23]
# END RECEIVE ORGTBL marks
}

## In case the above does not work (if org-version too old)

orig_table_2 = """

#+ORGTBL: SEND marks2 orgtbl-to-csv :skip 2
| Name   | T1 | T2 | T3 | T4 | Total |
| <6>    |    |    |    |    |       |
| abe    |  1 |  2 |  3 |  4 |    10 |
| beth   |  9 |  1 |  5 |  9 |    24 |
| catherine |  5 |  6 |  7 |  5 |    23 |
#+TBLFM: $6=$2+$3+$4+$5
"""

stud_db_2 = {
# Dont handedit
# BEGIN RECEIVE ORGTBL marks2
abe,1,2,3,4,10
beth,9,1,5,9,24
catherine,5,6,7,5,23
# END RECEIVE ORGTBL marks2



More information about the Python-list mailing list