如何解决Python子字符串函数从字符串中获取多个数据块
我有包含一些文本数据和一些 json 数据的数据字符串,现在我希望将此文本的所有 json 出现提取到单个变量中。 以下是原始数据
--fd709ff560be6440aafc1c097a8b6c8a Cache-Control: must-revalidate,no-cache,private Content-Id: <view_category> Content-Language: en Content-Type: application/vnd.api+json Date: Fri,18 Dec 2020 13:40:27 GMT Expires: Sun,19 Nov 1978 05:00:00 GMT Status: 200 X-Content-Type-Options: nosniff X-Drupal-Dynamic-Cache: MISS x-frame-options: SAMEORIGIN X-Generator: Drupal 8 (https://www.drupal.org) X-Ua-Compatible: IE=edge {"jsonapi":{
"Demo": "def","endDate": "/Date(1604508800000)/","startDate": "/Date(1608538800000)/","userIdNav": {
"__Metadata": {
"uri": "https://srv1.sapsf.com/odata/v2/User('xxs')","type": "SFOData.UserData"
}
},"timeTypeNav": {
"__Metadata": {
"uri": "https://srv1.sapsf.com/odata/v2/TimeType('sf')","type": "SFOData.TimeTypeDAta"
}
}
}} --fd709ff560be6440aafc1c097a8b6c8a Cache-Control: must-revalidate,private Content-Id: <create_product#body{0}> Content-Language: en Content-Type: application/vnd.api+json Date: Fri,18 Dec 2020 13:40:28 GMT Expires: Sun,19 Nov 1978 05:00:00 GMT Location: http://www.etyy.ert.yy.xxx/jsonapi/xxx/product/44b8a692-b831-4c70-842f-0c3196912c02 Status: 201 X-Content-Type-Options: nosniff x-frame-options: SAMEORIGIN X-Generator: Drupal 8 (https://www.drupal.org) X-Ua-Compatible: IE=edge {"jsonapi":{
"Demo": "Dump","endDate": "/Date(1604208800000)/","startDate": "/Date(1508538800000)/","type": "SFOData.TimeTypeDAta"
}
}
}--fd709ff560be6440aafc1c097a8b6c8a--
这里是python代码
def find_between( myrawData,first,last ):
try:
start = myrawData.index( first ) + len( first )
end = myrawData.index( last,start )
return myrawData[start:end]
except ValueError:
return ""
def find_between_r( myrawData,last ):
try:
start = myrawData.rindex( first ) + len( first )
end = myrawData.rindex( last,start )
return myrawData[start:end]
except ValueError:
return ""
if __name__ == "__main__":
first_rdata=find_between_r(myVarStr,'X-Ua-Compatible: IE=edge','--')
print(first_rdata)
我想获得以下输出,其中包含来自字符串的两个 json 数据作为组合一个它可以是字符串也可以。
{
"jsonapi": {
"Demo": "def","userIdNav": {
"__Metadata": {
"uri": "https://srv1.sapsf.com/odata/v2/User('xxs')","type": "SFOData.UserData"
}
},"timeTypeNav": {
"__Metadata": {
"uri": "https://srv1.sapsf.com/odata/v2/TimeType('sf')","type": "SFOData.TimeTypeDAta"
}
}
},"jsonapi": {
"Demo": "Dump","userIdNav": {
"__Metadata": {
"uri": "https://srv2.sapsf.com/odata/v2/User('xxs')","timeTypeNav": {
"__Metadata": {
"uri": "https://srv2.sapsf.com/odata/v2/TimeType('sf')","type": "SFOData.TimeTypeDAta"
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。