I have a list that i retrieve from database and it has two value:
list = [('[email protected]', '[email protected]\n', '{ "header1": "Subject", "header2": "Text"}', ' { "condition1": "Equal", "condition2": "Contain"}', '{ "parameter1": "hi1", "parameter2": "hi2" || "testNested"}', '{ "subjectP1": "WorkedDynamicWord","wordP1": "hi1","subjectP2": "WorkedDynamicWord2", "wordP2": "Dynamic word"}'), ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)]
As you can see, the two different value is differentiate with the comma at ),
. So how can i seperate these two value and insert to a variable for each list value?
For example (expected output):
variableA = ('[email protected]', '[email protected]\n', '{ "header1": "Subject", "header2": "Text"}', ' { "condition1": "Equal", "condition2": "Contain"}', '{ "parameter1": "hi1", "parameter2": "hi2" || "testNested"}', '{ "subjectP1": "WorkedDynamicWord","wordP1": "hi1","subjectP2": "WorkedDynamicWord2", "wordP2": "Dynamic word"}')
variableB = ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)
The variable will be add dynamically if the database has third value. For example, variableC will store the third value and variableD will be generate if there is fourth value.
CodePudding user response:
You can create variables as such
data = [('[email protected]', '[email protected]\n', '{ "header1": "Subject", "header2": "Text"}', ' { "condition1": "Equal", "condition2": "Contain"}', '{ "parameter1": "hi1", "parameter2": "hi2" || "testNested"}', '{ "subjectP1": "WorkedDynamicWord","wordP1": "hi1","subjectP2": "WorkedDynamicWord2", "wordP2": "Dynamic word"}'), ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)]
for i,l in enumerate(data):
vars()[f'variable{i 1}'] = l
print(variable1, variable2)
CodePudding user response:
Python’s globals() function returns a dictionary containing the current global symbol table, so you can add your variables with custom names:
data = [('[email protected]', '[email protected]\n', '{ "header1": "Subject", "header2": "Text"}', ' { "condition1": "Equal", "condition2": "Contain"}', '{ "parameter1": "hi1", "parameter2": "hi2" || "testNested"}', '{ "subjectP1": "WorkedDynamicWord","wordP1": "hi1","subjectP2": "WorkedDynamicWord2", "wordP2": "Dynamic word"}'), ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)]
for iVariable, variable in enumerate(data):
globals()["variable" str(iVariable 1)] = variable
print("Variable 1 : ", variable1)
print("Variable 2 : ",variable2)
The output is:
Variable 1 : ('[email protected]', '[email protected]\n', '{ "header1": "Subject", "header2": "Text"}', ' { "condition1": "Equal", "condition2": "Contain"}', '{ "parameter1": "hi1", "parameter2": "hi2" || "testNested"}', '{ "subjectP1": "WorkedDynamicWord","wordP1": "hi1","subjectP2": "WorkedDynamicWord2", "wordP2": "Dynamic word"}')
Variable 2 : ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)
If you prefer to have letters (variableA, variableB, ...), replace str(iVariable 1)
by chr(ord('@') iVariable 1)
that will convert the number to corresponding alphabetic character.