Extracting a determined value from a list
Question:
I have a list from which I want to extract a number which corresponds to totalsec. I proceeded with the split method to constitute a list of list and then extract the totalsec but I got an error
"list has no attribute split".
Here is my list:
['nbresets:0,totalsec:14,lossevtec:0,lossevt:0,sevlossec:0,sevloss:0,paclosevtec:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,paclosevt:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,dellosevtec:0:0:0:0:0:0:0:0:0:0:0,dellosevt:0:0:0:0:0:0:0:0:0:0:0,dursevlossec:0:0:0:0:0:0:0:0:0:0:0:0,dursevloss:0:0:0:0:0:0:0:0:0:0:0:0,nbfailures:0,notif:2']
the number I want to extract from this list is 14.
Please if you have any Idea how to do that please help!
Answers:
You cannot perform a split directly on the list, you can only perform a split on a string (since the list is already split). So iterate over your list of strings and split each of them.
I will shorten the code to make it more readable, just replace it with your list.
strings = ['nbresets:0,totalsec:14,lossevtec:0,lossevt:0']
for s in strings:
# first split by comma to get the single entries
for line in s.split(','):
params = line.split(':')
if params[0] == 'totalsec':
print(params[1]) # this should return 14
import re
my_lst = ['nbresets:0,totalsec:14,lossevtec:0,lossevt:0,sevlossec:0,sevloss:0,paclosevtec:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,paclosevt:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,dellosevtec:0:0:0:0:0:0:0:0:0:0:0,dellosevt:0:0:0:0:0:0:0:0:0:0:0,dursevlossec:0:0:0:0:0:0:0:0:0:0:0:0,dursevloss:0:0:0:0:0:0:0:0:0:0:0:0,nbfailures:0,notif:2']
print re.findall(pattern="totalsec:(d+)", string=my_lst[0])[0]
output:
14
Why are you using a list as container? Your list has only one entry (everything between ‘ ‘). And you can only use split on a string.
Just split the string into a list. Then iterate over the list and extract the key / value pairs by again splitting:
myString = 'nbresets:0,totalsec:14,lossevtec:0,lossevt:0,sevlossec:0,sevloss:0,paclosevtec:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,paclosevt:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,dellosevtec:0:0:0:0:0:0:0:0:0:0:0,dellosevt:0:0:0:0:0:0:0:0:0:0:0,dursevlossec:0:0:0:0:0:0:0:0:0:0:0:0,dursevloss:0:0:0:0:0:0:0:0:0:0:0:0,nbfailures:0,notif:2'
result = {}
for item in myString.split(","):
key, value = item.split(':', 1)
result[key] = value
print(result['totalsec'])
You can try this also:-
for i in a: #a is your list of data here
gen = i.split(",")
result = [x for x in gen if x.startswith('totalsec')] #which can be dynamic
for st in result:
name,value = st.split(":")
print(value)
I have a list from which I want to extract a number which corresponds to totalsec. I proceeded with the split method to constitute a list of list and then extract the totalsec but I got an error
"list has no attribute split".
Here is my list:
['nbresets:0,totalsec:14,lossevtec:0,lossevt:0,sevlossec:0,sevloss:0,paclosevtec:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,paclosevt:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,dellosevtec:0:0:0:0:0:0:0:0:0:0:0,dellosevt:0:0:0:0:0:0:0:0:0:0:0,dursevlossec:0:0:0:0:0:0:0:0:0:0:0:0,dursevloss:0:0:0:0:0:0:0:0:0:0:0:0,nbfailures:0,notif:2']
the number I want to extract from this list is 14.
Please if you have any Idea how to do that please help!
You cannot perform a split directly on the list, you can only perform a split on a string (since the list is already split). So iterate over your list of strings and split each of them.
I will shorten the code to make it more readable, just replace it with your list.
strings = ['nbresets:0,totalsec:14,lossevtec:0,lossevt:0']
for s in strings:
# first split by comma to get the single entries
for line in s.split(','):
params = line.split(':')
if params[0] == 'totalsec':
print(params[1]) # this should return 14
import re
my_lst = ['nbresets:0,totalsec:14,lossevtec:0,lossevt:0,sevlossec:0,sevloss:0,paclosevtec:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,paclosevt:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,dellosevtec:0:0:0:0:0:0:0:0:0:0:0,dellosevt:0:0:0:0:0:0:0:0:0:0:0,dursevlossec:0:0:0:0:0:0:0:0:0:0:0:0,dursevloss:0:0:0:0:0:0:0:0:0:0:0:0,nbfailures:0,notif:2']
print re.findall(pattern="totalsec:(d+)", string=my_lst[0])[0]
output:
14
Why are you using a list as container? Your list has only one entry (everything between ‘ ‘). And you can only use split on a string.
Just split the string into a list. Then iterate over the list and extract the key / value pairs by again splitting:
myString = 'nbresets:0,totalsec:14,lossevtec:0,lossevt:0,sevlossec:0,sevloss:0,paclosevtec:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,paclosevt:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0,dellosevtec:0:0:0:0:0:0:0:0:0:0:0,dellosevt:0:0:0:0:0:0:0:0:0:0:0,dursevlossec:0:0:0:0:0:0:0:0:0:0:0:0,dursevloss:0:0:0:0:0:0:0:0:0:0:0:0,nbfailures:0,notif:2'
result = {}
for item in myString.split(","):
key, value = item.split(':', 1)
result[key] = value
print(result['totalsec'])
You can try this also:-
for i in a: #a is your list of data here
gen = i.split(",")
result = [x for x in gen if x.startswith('totalsec')] #which can be dynamic
for st in result:
name,value = st.split(":")
print(value)