Skip to content

Instantly share code, notes, and snippets.

@timothymillar
Created November 5, 2018 21:13
Show Gist options
  • Save timothymillar/e12986dd4d8d380805cda5ae3d4169ce to your computer and use it in GitHub Desktop.
Save timothymillar/e12986dd4d8d380805cda5ae3d4169ce to your computer and use it in GitHub Desktop.
Read the result of a shell command into a pandas dataframe
#! /usr/bin/env python3
import io
import subprocess
import pandas
def read_shell(command, **kwargs):
"""
Takes a shell command as a string and and reads the result into a Pandas DataFrame.
Additional keyword arguments are passed through to `pandas.read_table`.
:param command: a shell command that returns tabular data
:type command: str
:return: a pandas datafram
:rtype: :class:`pandas.dataframe`
"""
proc = subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output, error = proc.communicate()
if proc.returncode == 0:
with io.StringIO(output.decode()) as buffer:
return pandas.read_table(buffer, **kwargs)
else:
message = ("Shell command returned non-zero exit status: {0}\n\n"
"Command was:\n{1}\n\n"
"Standard error was:\n{2}")
raise IOError(message.format(proc.returncode, command, error.decode()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment