[Code] Functions to Convert a Text Data Table to Working Python Code in EMACS

This is EMACS code and text editor LISP code to convert a space delimited text table into working Python code in EMACS. By default, it binds the python-table function below to the CTRL-= key sequence in EMACS for easy use. Add this code to your dot emacs EMACS startup file (~/.emacs). Restart EMACS to load the new dot emacs startup file.

Select the text data table in EMACS using rectangle-mark-mode — bound to CTRL-SPACEBAR by default — and press CTRL-= to convert the selected text data table to a Python NumPy fast numerical array definition. The emacs LISP function python-table automates the steps shown in the previous post “[Video] How to Copy and Paste Data Tables into Working Python Code with EMACS.”

(defun python-table-rows (inputStr)
  "Convert space delimited text table to working Python fast NumPy array definition code"
  (interactive "e")
  (setq head "table_name = np.array([\n")
  ;; replace thousands separator comma (,) with Python separator underscore (_)
  (setq temp1 (replace-regexp-in-string "," "_" inputStr))
  ;; enclose each row in [ (row) ],
;;  (setq temp2 (replace-regexp-in-string "\\(.*\\)" "[\\1]," temp1))
  (setq temp2 (replace-regexp-in-string "\\([a-zA-Z0-9_ \\.]*\\)" "[\\1]," temp1))
  ;; remove any spurious [],
  (setq temp2b (replace-regexp-in-string "\\[\\]," "" temp2))
  ;; convert repeated space delimeters to (comma)(space)
  (setq temp3 (replace-regexp-in-string " +" ", " temp2b))
  ;;  add ] to close list of lists, close paren ) to convert to numpy array
  (setq tail "])")
  ;; build entire Python code block
  (setq temp4 (concat head temp3))
  (setq temp5 (concat temp4 tail))
  ) ;; end defun python-table

(defun python-table ()
  " convert selected region with space separated text table to python code "
  (interactive)
  (message "running python-table") ;; progress message
  ;; use buffer-substring-no-properties to strip fonts etc.
  ;; get text from selected region and put in tmp variable
  (setq tmp (buffer-substring-no-properties (mark) (point)))
  ;; convert to Python Code and put in table variable
  (setq table (python-table-rows tmp))
  (message table) ;; progress message
  (delete-region (mark) (point))  ;; remove the region contents
  (insert table)  ;; replace with python code table definition
  ) ;; end defun ptable()

(global-set-key (kbd "C-=") 'python-table) ;; bind python-table to hot key

Use CTRL-X CTRL-F ~/.emacs RETURN to read in, display, and edit your dot emacs file in the EMACS editor.

How to Use

Add this code to your dot emacs file. Modify as appropriate if you know what you are doing.

Restart emacs.

Use the EMACS rectangle-mark-mode command — usually bound to CTRL-X SPACEBAR in EMACS — to select a rectangular text table as in the text below. Use Edit Menu | Copy or ESC-W to copy the text data table in EMACS.

This is an example of selecting, copying, and pasting a text table
into Python source code using the Emacs code and text editor.

Python is a popular programming language. With add on packages
such as NumPy, SciPy, and Matplotlib, it is a leading tool
for data analysis, scientific and numerical programming.

Demo Text Table
0.5      276
2.5      328
6.5      134
12.0     139
17.0   1,807 random notes here
22.0   3,342
29.5   5,340 commentary here
39.5   3,316
49.5   2,106
59.5   1,360 doodling here
69.5     562
79.5     270
89.5     120

This data is so great.

Three ways to select a rectangular text region in Emacs:

ESC-x rectangle-mark-mode
CTRL-X SPACE
SHIFT-(mouse drag)

URL: http://www.mathematical-software.com/

Use CTRL-y to paste the selected text table into Python code. Select the table and use CTRL-= to convert to working Python source code — a NumPy fast array definition with the values from the text table. The Python add-on packages NumPy, SciPy and MatPlotLib provide extensive numerical and statistical analysis functions and plotting.

(C) 2021 by John F. McGowan, Ph.D.

About Me

John F. McGowan, Ph.D. solves problems using mathematics and mathematical software, including developing gesture recognition for touch devices, video compression and speech recognition technologies. He has extensive experience developing software in C, C++, MATLAB, Python, Visual Basic and many other programming languages. He has been a Visiting Scholar at HP Labs developing computer vision algorithms and software for mobile devices. He has worked as a contractor at NASA Ames Research Center involved in the research and development of image and video processing algorithms and technology. He has published articles on the origin and evolution of life, the exploration of Mars (anticipating the discovery of methane on Mars), and cheap access to space. He has a Ph.D. in physics from the University of Illinois at Urbana-Champaign and a B.S. in physics from the California Institute of Technology (Caltech).