i want to know how to assign the output of print to a variable.
so if
mystring = \"a=\\\'12\\\'\"
then
print mys
I wouldn't do it that way, personally. A far less hackish solution is to build a dictionary from your data first, and then pass it whole to a function as **kwargs
. For example (this isn't the most elegant way to do it, but it is illustrative):
import re
remove_non_digits = re.compile(r'[^\d.]+')
inputList = ["a='0.015in' lPrime='0.292' offX='45um' offY='75um' sPrime='0.393' twistLength='0'",
"a='0.015in' lPrime='0.292' offX='60um' offY='75um' sPrime='0.393' twistLength='0'",
"a='0.015in' lPrime='0.292' offX='75um' offY='75um' sPrime='0.393' twistLength='0'", '']
#remove empty strings
flag = True
while flag:
try:
inputList.remove('')
except ValueError:
flag=False
outputList = []
for varString in inputList:
varStringList = varString.split()
varDict = {}
for aVar in varStringList:
varList = aVar.split('=')
varDict[varList[0]] = varList[1]
outputList.append(varDict)
for aDict in outputList:
for aKey in aDict:
aDict[aKey] = float(remove_non_digits.sub('', aDict[aKey]))
print outputList
This prints:
[{'a': 0.014999999999999999, 'offY': 75.0, 'offX': 45.0, 'twistLength': 0.0, 'lPrime': 0.29199999999999998, 'sPrime': 0.39300000000000002}, {'a': 0.014999999999999999, 'offY': 75.0, 'offX': 60.0, 'twistLength': 0.0, 'lPrime': 0.29199999999999998, 'sPrime': 0.39300000000000002}, {'a': 0.014999999999999999, 'offY': 75.0, 'offX': 75.0, 'twistLength': 0.0, 'lPrime': 0.29199999999999998, 'sPrime': 0.39300000000000002}]
Which appears to be exactly what you want.