Create a gist now

Instantly share code, notes, and snippets.

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.
(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))
(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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment