I appologize if this has been answered elsewhere but the terms I search for do not turn up a good fit.

I am new to everything I am trying to do here and need some direction to solve this problem.

I have a string that is 'variable1=5&varible2=3&variable3=27&varible4=1'

I need to be able to make this useful so I can do something like:
variable5 = variable1 + variable2

I have seen similiar(but not similiar enough) questions on other sites and the question never gets answered. People usually just shout at the person that what they want to do is dangerous so they are not going to help.

This is written on a stand alone linux terminal that will be placed on an industrial machine that will gather data from a PLC on the machine and send/recieve data from a website. This is the format that the data comes to me. The terminal will not be connected directly to any I/O so security is not a big concern.

I wouldn't pollute the main namespace with arbitrary variables. I provided this in comments.

First you have to convert the input string into a dictionary.
Then you can update some objects namespace with this dictionary.

class Importedvalues(object): pass

i=Importedvalues()

vs='variable1=5&varible2=3&variable3=27&varible4=1'
vs_modified=dict()
for statement in vs.split("&"):
    varname,value=statement.split("=")
    vs_modified[varname]=value

#locals().update(vs_modified)
vars(i).update(vs_modified)

print i.variable1
print i.varible2
print i.varible4

#print variable1
#print varible2
#print varible4

Or better:

class Importedvalues(object): 
    def __init__(self,inputstring):
        vs_modified=dict()
        for statement in inputstring.split("&"):
            varname,value=statement.split("=")
            vs_modified[varname]=value
        self.__dict__.update(vs_modified)

vs='variable1=5&varible2=3&variable3=27&varible4=1'
i=Importedvalues(vs)

print i.variable1
print i.varible2
print i.varible4

Good idèe bye slate to update objects namespace.
You can also just work with dictionary if you need result variable2 + variable3 wish is 30.
Herer a little compressed version.

>>> s = 'variable1=5&varible2=3&variable3=27&varible4=1'
>>> d = dict((k[0], k[1]) for k in [i.split('=') for i in s.split('&')])
>>> int(d['varible2']) + int(d['variable3'])
30

Edited 2 Years Ago by snippsat

This question has already been answered. Start a new discussion instead.