2

I have a CSV data stream/object called jobresults:

"number","person1","person2","type"
1234,"Michael Scott","Pam Beasley",false
2345,"Michael Scott","Jim Halpert",true
3456,"Jim Halpert","Dwight Schrute",false

How do I convert this object (it is NOT written out to a file) to a Pandas DataFrame?

I tried:

df = pd.read_csv(jobresults)

...to no avail. I believe read_csv requires an actual file pulled from the OS.

Any insight will be much appreciated!

1 Answer 1

6

Use io.StringIO

As stated here

For Python 3 use

from io import StringIO

df = pd.read_csv(StringIO(jobresults))

For Python 2 use

from StringIO import StringIO

df = pd.read_csv(StringIO(jobresults))

Considering your object is a splunklib.binding.ResponseReader You may need to use the read() method...

Try this:

from StringIO import StringIO

df = pd.read_csv(StringIO(jobresults.read()))
Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting this error: File "<ipython-input-39-64e491fd9bb9>", line 69, in <module> df = pd.read_csv(StringIO(jobresults)) TypeError: initial_value must be unicode or None, not ResponseReader I'm using Python 2.7 if that matters.
What is type(jobresults)?
splunklib.binding.ResponseReader - it is a feed from a Splunk query
Perfect - thank you very much. Apologies for the simple question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.