Skip to content

Instantly share code, notes, and snippets.

@andrewgiessel
Created May 25, 2012 00:49
Show Gist options
  • Save andrewgiessel/2785137 to your computer and use it in GitHub Desktop.
Save andrewgiessel/2785137 to your computer and use it in GitHub Desktop.
copy/paste numpy arrays to/from excel (os x)
import subprocess
import tempfile
import numpy as np
import os
def npArrayFromClipboard():
"""Reads the current clipboard and tries to make an numpy array out of it"""
p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
output = p.communicate()
return np.array([i.split('\t') for i in output[0].split('\r')])
def openArrayInExcel(A):
"""create a temporary file from a 1 or 2D numpy array and open it in excel.
works on OS X"""
t = tempfile.NamedTemporaryFile(suffix='.csv', delete=False)
np.savetxt(t.file, A, delimiter=',')
t.file.close()
os.system('open ' + t.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment