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 :

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


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

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]

Edited 3 Years Ago by woooee

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


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

This article has been dead for over six months. Start a new discussion instead.