Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert text string to dataframe - comma separated

Tags:

python

pandas

I am trying to parse the response of an API. Takes a batch of phone numbers, and returns information on their status i.e. active or not.

This is what the response looks like:

# API call

s.get('https://api/data/stuff')

# response

',MSISDN,Status,Error Code,Error Text,Original Network,Current Network,Current Country,Roaming 
Country,Type,Date Checked\n447541255456,447541255456,Undelivered,27,Absent Subscriber,O2 
(UK),,,,Mobile,Wed Oct  9 2019 12:26:51 GMT+0000 (UTC)\n447856999555,447856999555,Undelivered,1,Dead,O2
 (UK),,,,Mobile,Wed Oct  9 2019 12:26:51 GMT+0000 
(UTC)\n447854111222,447854111222,Undelivered,1,Dead,Orange,,,,Mobile,Wed Oct  9 2019 12:26:51 GMT+0000 
(UTC)\n'

I can see that MSISDN,Status,Error Code,Error Text,Original Network,Current Network,Current Country,Roaming Country,Type,Date Checked are headers, and the rest are the rows.

But I can't get this into a structure I can read easily, such as a dataframe.

There were some suggested answers while typing this question, which use import io and pd.read_table etc. but I couldn't get any of them to work.

I guess I could save it as a txt file then read it back in as a comma separated csv. But is there a native pandas or other easier way to do this?

Here's the response string pasted directly into stack overflow with no tidying:

',MSISDN,Status,Error Code,Error Text,Original Network,Current Network,Current Country,Roaming Country,Type,Date Checked\n447541255456,447541255456,Undelivered,27,Absent Subscriber,O2 (UK),,,,Mobile,Wed Oct  9 2019 12:26:51 GMT+0000 (UTC)\n447856999555,447856999555,Undelivered,1,Dead,O2 (UK),,,,Mobile,Wed Oct  9 2019 12:26:51 GMT+0000 (UTC)\n447854111222,447854111222,Undelivered,1,Dead,Orange,,,,Mobile,Wed Oct  9 2019 12:26:51 GMT+0000 (UTC)\n'
like image 397
SCool Avatar asked Jul 24 '26 11:07

SCool


1 Answers

I believe you need:

from io import StringIO

df = pd.read_csv(StringIO(s.get('https://api/data/stuff')))

Or try:

df = pd.read_csv('https://api/data/stuff')
like image 129
jezrael Avatar answered Jul 27 '26 12:07

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!