Hi, i have this output from a SQL query :
new = (('{reason:slash,data:{"SET0":"=SET2"}}',), ('{reason:slash,data:{"""REM_1""": """=0xFF""", """REM_2""": """=0x1""", """SET0:REM_3""": """=SET2:REM_3""", """MREM_4""": """=0xFF"""};{}}',))

I am trying to get it such that i put all these items in a dic in the following manner :

{'MREM_4': '=0xFF', 'REM_2': '=0x1', 'REM_1': '=0xFF', 'SET0:REM_3': '=SET2:REM_3'}

THe way i went about doing this is :

trace={}    
for reason in new:
    for oper in reason:
        data = oper.split(';')

trace.update(eval(data.pop(0).split('data:')[1].split(';')[0]))

>>> print trace
{'MREM_4': '=0xFF', 'REM_2': '=0x1', 'REM_1': '=0xFF', 'SET0:REM_3': '=SET2:REM_3'}

I am wondering if there is a better way to do it without using the pop function, if for eg the data:{'SET0':'SET2'} doesnt appear as the first entry, than the above will break since i will be poping the wrong item. Any suggestion is appreciated, thanks

Recommended Answers

All 2 Replies

Once you get to the string you want to use you can split it

new_str = '{reason:slash,data:{"""REM_1""": """=0xFF""", """REM_2""": """=0x1""", """SET0:REM_3""": """=SET2:REM_3""", """MREM_4""": """=0xFF"""};{}}'

x= new_str.split('"""')
print x[13], x[15]
print x[5], x[7]

understand, but am wondering how you will arrive at your new_str. The way i did it was to use the

data=oper.split(';')

which gives me a list and than i went on to use the pop to remove the first item, reason:slash,data:{SET0:SET2}. I wanted to know if there is a better way to do it without using the pop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.