json 在Python中将结构化符号文本转换为对象

e37o9pze  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(175)

我有一个来自API调用的响应,它为我提供了一种形式的点符号文本。我希望能够将点符号文本转换为我可以轻松操作的东西-比如对象。点符号文本是不对称的,因此描述特定功能的点符号的“深度”是可变的。示例文本:

table.VideoAnalyseRule[0][0].Class=Normal
table.VideoAnalyseRule[0][0].Config.Action[0]=Appear
table.VideoAnalyseRule[0][0].Config.Action[1]=Cross
table.VideoAnalyseRule[0][0].Config.DetectRegion[0][0]=764
table.VideoAnalyseRule[0][0].Config.DetectRegion[0][1]=6089
table.VideoAnalyseRule[0][0].Config.DetectRegion[1][0]=2694
table.VideoAnalyseRule[0][0].Config.DetectRegion[1][1]=5991
table.VideoAnalyseRule[0][0].Config.DetectRegion[2][0]=2603
table.VideoAnalyseRule[0][0].Config.DetectRegion[2][1]=7902
table.VideoAnalyseRule[0][0].Config.DetectRegion[3][0]=637
table.VideoAnalyseRule[0][0].Config.DetectRegion[3][1]=7805
table.VideoAnalyseRule[0][0].Config.Direction=Both
table.VideoAnalyseRule[0][0].Config.MaxTargets=15
table.VideoAnalyseRule[0][0].Config.MinDuration=6
table.VideoAnalyseRule[0][0].Config.MinTargets=1
table.VideoAnalyseRule[0][0].Config.ReportInterval=0
table.VideoAnalyseRule[0][0].Config.Sensitivity=5
table.VideoAnalyseRule[0][0].Config.SizeFilter.CalibrateBoxs[0].CenterPoint[0]=4096
table.VideoAnalyseRule[0][0].Config.SizeFilter.CalibrateBoxs[0].CenterPoint[1]=4096
table.VideoAnalyseRule[0][0].Config.SizeFilter.CalibrateBoxs[0].Ratio=1
table.VideoAnalyseRule[0][0].Config.SizeFilter.MaxSize[0]=8191
table.VideoAnalyseRule[0][0].Config.SizeFilter.MaxSize[1]=8191
table.VideoAnalyseRule[0][0].Config.SizeFilter.MinSize[0]=0
table.VideoAnalyseRule[0][0].Config.SizeFilter.MinSize[1]=0
table.VideoAnalyseRule[0][0].Config.SizeFilter.Type=ByLength
table.VideoAnalyseRule[0][0].Config.TrackDuration=30
table.VideoAnalyseRule[0][0].Enable=true
table.VideoAnalyseRule[0][0].EventHandler.AlarmOutChannels[0]=0
table.VideoAnalyseRule[0][0].EventHandler.AlarmOutEnable=false
table.VideoAnalyseRule[0][0].EventHandler.AlarmOutLatch=10
table.VideoAnalyseRule[0][0].EventHandler.BeepEnable=false
table.VideoAnalyseRule[0][0].EventHandler.Dejitter=0
table.VideoAnalyseRule[0][0].EventHandler.Delay=30
table.VideoAnalyseRule[0][0].EventHandler.ExAlarmOutChannels[0]=0
table.VideoAnalyseRule[0][0].EventHandler.ExAlarmOutEnable=false
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.Enable=false
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.FilckerIntevalTime=5
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.FilckerLightType=RedBlueLight
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.LightDuration=10
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.LightLinkType=Filcker
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[0][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[0][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[0][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[0][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[0][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[0][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[1][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[1][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[1][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[1][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[1][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[1][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[2][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[2][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[2][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[2][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[2][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[2][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[3][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[3][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[3][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[3][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[3][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[3][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[4][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[4][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[4][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[4][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[4][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[4][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[5][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[5][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[5][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[5][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[5][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[5][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[6][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[6][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[6][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[6][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[6][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LightingLink.WhiteLightTimeSection[6][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.LogEnable=false
table.VideoAnalyseRule[0][0].EventHandler.LogRemote=true
table.VideoAnalyseRule[0][0].EventHandler.MMSEnable=false
table.VideoAnalyseRule[0][0].EventHandler.MailEnable=true
table.VideoAnalyseRule[0][0].EventHandler.MatrixChannels[0]=0
table.VideoAnalyseRule[0][0].EventHandler.MatrixEnable=false
table.VideoAnalyseRule[0][0].EventHandler.MessageEnable=false
table.VideoAnalyseRule[0][0].EventHandler.PtzLink[0][0]=None
table.VideoAnalyseRule[0][0].EventHandler.PtzLink[0][1]=0
table.VideoAnalyseRule[0][0].EventHandler.PtzLink[0][2]=0
table.VideoAnalyseRule[0][0].EventHandler.PtzLink[0][3]=0
table.VideoAnalyseRule[0][0].EventHandler.PtzLink[0][4]=0
table.VideoAnalyseRule[0][0].EventHandler.PtzLinkEnable=false
table.VideoAnalyseRule[0][0].EventHandler.RecordChannels[0]=0
table.VideoAnalyseRule[0][0].EventHandler.RecordEnable=true
table.VideoAnalyseRule[0][0].EventHandler.RecordLatch=10
table.VideoAnalyseRule[0][0].EventHandler.SnapshotChannels[0]=0
table.VideoAnalyseRule[0][0].EventHandler.SnapshotEnable=true
table.VideoAnalyseRule[0][0].EventHandler.SnapshotPeriod=0
table.VideoAnalyseRule[0][0].EventHandler.SnapshotTimes=1
table.VideoAnalyseRule[0][0].EventHandler.SnapshotTitleEnable=true
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[0][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[0][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[0][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[0][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[0][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[0][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[1][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[1][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[1][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[1][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[1][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[1][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[2][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[2][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[2][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[2][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[2][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[2][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[3][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[3][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[3][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[3][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[3][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[3][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[4][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[4][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[4][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[4][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[4][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[4][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[5][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[5][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[5][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[5][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[5][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[5][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[6][0]=1 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[6][1]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[6][2]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[6][3]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[6][4]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.TimeSection[6][5]=0 00:00:00-23:59:59
table.VideoAnalyseRule[0][0].EventHandler.Voice.AudioFileName=/usr/data/audiofiles/audios/alarm.wav
table.VideoAnalyseRule[0][0].EventHandler.Voice.PlayTimes=3
table.VideoAnalyseRule[0][0].EventHandler.VoiceEnable=false
table.VideoAnalyseRule[0][0].Id=1
table.VideoAnalyseRule[0][0].Name=Test Rule
table.VideoAnalyseRule[0][0].ObjectTypes[0]=Human
table.VideoAnalyseRule[0][0].ObjectTypes[1]=Vehicle
table.VideoAnalyseRule[0][0].PtzPresetId=0
table.VideoAnalyseRule[0][0].TrackEnable=false
table.VideoAnalyseRule[0][0].Type=CrossRegionDetection

字符串
这个输出来自一个CCTV摄像机。这个“表”可能有多个“规则”,基于.Class或.Config等之前的[channel number][rule number]表示法。例如,table.VideoAnalyseRule[0][1]将是第一个通道上的第二个规则(从零开始计数)。table.VideoAnalyseRule[1][0]将是第二个通道,第一个规则,依此类推。
目标是有一些东西,我可以用来操纵任何特定规则的设置,例如将Enable的值从0更改为1,或更改名称,或计划,或日志记录,然后,我会创建必要的API调用,将数据输出到目标CCTV摄像机,并进行任何更改-输出格式基本上是JSON,如果我想发送命令到设备。
有没有一个Python库可以处理这类数据?有没有一种“Python”的方式来处理这类信息?我需要为此编写一个自定义解析器吗?
任何建议都非常感谢。

ukqbszuj

ukqbszuj1#

我希望能够做的是将点符号文本转换为我可以轻松操作的东西-比如对象。
以下代码处理示例输入。如果实际输入更一般,您可能需要调整它。此外,您可能需要自定义它,例如将所有数字字符串转换为数字。

import re, json

root = None

def getset_thing_denoted_by(accessor, initial_value):
    # Return the entity denoted by {accessor}.
    # If it doesn't yet exist, ensure that it does,
    # using {initial_value} as its value.

    if (mo := re.fullmatch(r'(\w+)', accessor)):
        # accessing the root

        [root_name] = mo.groups()
        global root
        if root is None:
            root = initial_value

        return root

    elif (mo := re.fullmatch(r'(\S+)\.(\w+)', accessor)):
        # accessing a field of an 'object'

        (object_accessor, field_name) = mo.groups()

        # We represent the object with a dict.
        obj = getset_thing_denoted_by(object_accessor, {})

        # The field might not yet exist
        if field_name not in obj:
            obj[field_name] = initial_value

        return obj[field_name]

    elif (mo := re.fullmatch(r'(\S+)\[(\d+)]', accessor)):
        # accessing an element of an 'array'

        (array_accessor, index_str) = mo.groups()

        # We represent an array as a list.
        arr = getset_thing_denoted_by(array_accessor, [])
        index = int(index_str)

        # The indexed element might not yet exist.
        assert len(arr) in [index, index+1]
        if len(arr) == index:
            arr.append(initial_value)

        assert len(arr) == index+1
        return arr[index]

    else:
        assert 0, f"Can't handle accessor: {accessor}"

for line in open('sample.txt'):
    (lhs, rhs) = re.fullmatch(r'(\S+)=(.+)\n', line).groups()
    getset_thing_denoted_by(lhs, rhs)

print(json.dumps(root, indent=4))

字符串

相关问题