微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python子字符串函数从字符串中获取多个数据块

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?