Reputation: 55
I already got the component value like this:
components = "Set(['LPE', 'CLK'])"
I don't want to be like this:
print components
Set(['LPE', 'CLK'])
How can I print/get value like this only?
LPE
CLK
Upvotes: 0
Views: 98
Reputation: 55
This can be solve by doing split:
components = "Set(['LPE', 'CLK'])"
x = components.lstrip("Set([").rstrip("])")
for i in x.split(", "):
z = i.lstrip("'").rstrip("'")
print z
Upvotes: 0
Reputation: 8400
Its easy to use eval
function to do this,
The eval
function lets a python program run python code
within itself.
var="set(['LPE', 'CLK'])"
print eval(a)
Output:
set(['LPE', 'CLK'])
Then use for loop,
for i in set(['LPE', 'CLK']):
print i
Output:
LPE
CLK
>>> for i in eval("set(['LPE', 'CLK'])"):
... print i
...
LPE
CLK
Upvotes: 1
Reputation: 1681
>>> neglect
['(', ')', "'", ',', '[', ']', 'e', 't', 'S']
>>> result=""
>>> for words in components:
... if(words in neglect):
... if(words == ','):
... result = result + " "
... else:
... result = result + words
...
>>> result
'LPE CLK'
Upvotes: 1
Reputation: 16950
If Set is set:
>>> c = "set(['LPE', 'CLK'])"
>>> eval(c)
set(['LPE', 'CLK'])
>>> for i in eval(c):
... print i
...
LPE
CLK
>>>
Upvotes: 1
Reputation: 387
convert from set to list. component_list = list(set(['LPE','CLK'])) and print component_list[0], component_list[1]
Upvotes: 0