Created
May 25, 2012 00:49
-
-
Save andrewgiessel/2785137 to your computer and use it in GitHub Desktop.
copy/paste numpy arrays to/from excel (os x)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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