public
Created

A couple of functions from my emacs configuration that convert copy/pasted HTML tables and random TSV files into nested lists for use in various Lisps.

  • Download Gist
tsv-to-sexp.el
Emacs Lisp
1 2 3 4 5 6 7 8 9 10
(defun reformat-field (str)
"Trims leading/trailing whitespace from `str`, then converts it to a number if the string looks like a number."
(let ((s (s-trim str)))
(if (and (string-match "[\$\.,0-9]+" s) (= (match-end 0) (length s)))
(string-to-number (replace-regexp-in-string "[\$,]" "" s))
s)))
 
(defun tsv-to-sexp (tsv)
"Parses the string `tsv` as a tab-separated-value file, returning a sexp containing the values with strings converted to numbers where appropriate."
(-map (lambda (s) (-map 'reformat-field (s-split "\t" s))) (s-lines tsv)))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.