0

How do I split the string 'Notification id = 7427580 user-id = 1992' to obtain the Id numbers from the string only.

For example:

var NotificationID = 7427580 

var UserID = 1992

Thanks in advance!

Thanks for the fix:

temp = 'Notification id = 7427580 user-id = 1992'
   for s in temp.split():
      if (s.isdigit()):
         id_list.append(s)

print(id_list[0])
print(id_list[1])
Chase
  • 23
  • 3
  • 2
    Read up on regular expressions. They are a very useful tool to have in your toolbox. – NPE Aug 11 '18 at 05:10
  • To get slightly more specific than NPE, you can capture groups in regexp and in this case I would just capture all continuous sequences of numbers. – Spencer Aug 11 '18 at 05:13

1 Answers1

-1

You can use the following regular expression to obtain numbers assigned to names ending with 'id':

import re
text = '''var NotificationID = 7427580
var UserID = 1992'''
print(re.findall(r'id\s*=\s*(\d+)', text, flags=re.IGNORECASE))

This outputs:

['7427580', '1992']
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • You can apply head,sepa,tail= s.partition("Notification id ="), and then print(int(tail.split()[0]))). And you can do the same with "UserId =" – kantal Aug 11 '18 at 05:39