Read CSVs to org tables
  |   Source

Turning CSV files into beautiful org-mode tables

One thing I find myself doing a lot is working with CSV (comma separated value) files, I store a lot of my data in them. I also find that I need to edit them a lot, or check the values in them. I generally then want to use them in reports that I write, at least I like to put in samples of the data to give readers an indication of what I'm talking about.

Since I write almost everything in org-mode these days I wrote this little function, partially to teach myself more elisp and partially because it actually is useful for me day to day. It simply takes a file location from the user and formats a nice org mode style table.

Elisp code

(defun org-read-csv ()
  (interactive)
  (progn
    (find-file (read-file-name "CSV location:"))
    (org-mode)
    (goto-char (point-min))
    (insert "|")
    (while (re-search-forward "\n" nil t)
      (replace-match "|\n|"))
    (delete-char -1)
    (while (re-search-backward "," nil t)
      (replace-match "|"))
    (org-table-align)
    ))
Comments powered by Disqus