Last active
November 8, 2018 19:23
-
-
Save ackman678/5816598 to your computer and use it in GitHub Desktop.
Convert a space delimited table on the system clipboard into markdown table, such as when copying tabular results from matlab or a spreadsheet or R command line. Written to use macos shell cmd pbpaste. But should be reworked to use xclip or similar as an option for linux. This script is most enjoyable when combined with a os wide shortcut, such …
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
#! /usr/bin/env python | |
#pyMakeMarkdownTable.py | |
#Created by James B. Ackman 4/9/2013 | |
''' | |
A script to format a space or tab delimited table on the clipboard into a markdown formatted table onto the clipboard | |
''' | |
#import os, sys, re | |
#import urllib2 | |
import os, subprocess, re | |
def getClipboardData(): | |
p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE) | |
retcode = p.wait() | |
data = p.stdout.read() | |
return data | |
def setClipboardData(data): | |
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE) | |
p.stdin.write(data) | |
p.stdin.close() | |
retcode = p.wait() | |
s1 = getClipboardData() | |
s = s1.expandtabs(1) | |
s = re.sub(' +',' ',s) | |
sList = s.split('\n') | |
nSeparators = sList[0].count(' ') | |
nSeparatorsTotal = s.count(' ') | |
newStr = s.replace(' ', ' | ', nSeparatorsTotal) | |
row2 = ' --- |' * nSeparators + ' ---' | |
newStr2 = newStr.replace('\n', '\n' + row2 + '\n', 1) | |
#nLine = sList[0].replace(' ', ' | ', nSeparators) | |
#print str(row2) | |
setClipboardData(newStr2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment