3

I would like to know how to get all the value of a specific information. In this case, I want to get all the 'name' of a message and convert it in an array. Example of message:

"xxxxxxxxxx name:yyyy; xxxxxxxxxxxxxxxxxxx name:yyyyy;"

Code (I only got the first 'name'):

 for i in range(0,len(message)):  
     start = message.find('name') + 4
     end = message.find(';', start)
     a=message[start:end]
     split=a.split(';')
  print(split)
0

4 Answers 4

5

Using regex:

import re
s = "xxxxxxxxxx name:yyyy; xxxxxxxxxxxxxxxxxxx name:yyyyy;"
print(re.findall(r"name:[a-zA-Z]+", s))

Output:

['name:yyyy', 'name:yyyyy']
Sign up to request clarification or add additional context in comments.

1 Comment

You could also use lookbehind and lookahead: r"(?<=name:).*?(?=;)", and in case the region boundaries are also relevant, better use finditer to get the full groups.
2

If you want to use str.find, you should provide a starting position for start, like you did for end, e.g. the previous end or 0 in the first iteration. Then, continue until start is -1, i.e. not found.

end = 0
while True:
    start = message.find('name', end)
    if start == -1:
        break
    end = message.find(';', start)
    a = message[start + 5 : end]
    print(start, end, repr(a))

Or you could split the message at whitespaces and use a conditional list comprehension:

>>> [s[5:-1] for s in message.split() if s.startswith("name")]
['yyyy', 'yyyyy']

In practice, however, I would recommend using regular expressions as shown in the other answer.

Comments

1

Your logic almost works, you can change it to:

message = "xxxxxxxxxx name:yyyy; xxxxxxxxxxxxxxxxxxx name:zzzzzz;"

start = message.find('name')      # find first start
stop = message.find(';', start)   # find first stop after first start
names = []                        # all your found values get appended into this

while -1 < start < stop: # ensure start is found and stop is after it
    names.append(message[start+5:stop]) # add 5 start-pos to skip 'name:' and slice value
                                        # from message and put into names

    start = message.find('name',stop)   # find next start after this match
    if start == -1:                     # if none found, you are done
        stop = -1
    else:
        stop = message.find(';', start) # else find the end for this start

if start != -1:     # if your last ; is missing, the while above will find 
                    # start but no stop, in that case add all from last start pos to end
    names.append(message[start:])

print ( names)

Output:

['yyyy', 'zzzzzz']

Comments

1
def formatFunction(x):
    index_of_name = x.index('name')
    if index_of_name >= 0:
        return x[index_of_name:]
    else:
        return None

message_list = message.split(';')
message_list = message_list.map(formatFunction)
formated_list = [x for x in message_list if x]
print(formated_list)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.