将文本字符串转换为数据框-逗号分隔

2024-05-13 20:44:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试解析API的响应。获取一批电话号码,并返回有关其状态的信息,即活动或不活动。你知道吗

答案是这样的:

# 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'

我可以看到MSISDN,Status,Error Code,Error Text,Original Network,Current Network,Current Country,Roaming Country,Type,Date Checked是标题,其余的是行。你知道吗

但是我不能把它放到一个我能很容易读懂的结构中,比如一个数据帧。你知道吗

在输入这个问题时,有一些建议的答案,它们使用了import iopd.read_table等,但我无法让它们中的任何一个起作用。你知道吗

我想我可以将它保存为txt文件,然后以逗号分隔的csv格式读回。但是,有没有一个本土的熊猫或其他更容易做到这一点的方法?你知道吗

下面是直接粘贴到堆栈溢出中的响应字符串,无需整理:

',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'

Tags: statuserrornetworkcurrentmobilecountryoctutc