Reputation: 2971
My string contains large paragraph like this:
Line= "
Name = AB | 1-2 | Name
ID = CD | 3-4 | int
Stu = EF | 5-6 | Name
Email = GH | 7-8 | string
ID = IJ | 9-10 | int
Tea = KL | 1--12 | Name
Email = MN | 13-14 | Name
ID = OP | 1-2 | int "
I want to store information which come above ID into an array like this:
A[0] = Name = AB | 1-2 | Name
A[1] = Stu = EF | 5-6 | Name
Email = GH | 7-8 | string
A[2] = Tea = KL | 1--12 | Name
Email = MN | 13-14 | Name
The array should continue as I have more data in string which is large, the array should be made up automatically, Can someone help?
Upvotes: 0
Views: 184
Reputation: 59974
EDIT: Used your string.
Note: There's probably a cleaner way to do this.
You can use regex:
>>> import re
>>> Line = """
Name = AB | 1-2 | Name
ID = CD | 3-4 | int
Stu = EF | 5-6 | Name
Email = GH | 7-8 | string
ID = IJ | 9-10 | int
Tea = KL | 1--12 | Name
Email = MN | 13-14 | Name
ID = OP | 1-2 | int """
>>> Line = '\n'.join(i.lstrip() for i in Line.strip().splitlines())
>>> newlist = [i.strip('\n') for i in re.split(r'ID.*',Line)]
>>> print newlist[0]
Name = AB | 1-2 | Name
>>> print newlist[1]
Stu = EF | 5-6 | Name
Email = GH | 7-8 | string
>>> print newlist[2]
Tea = KL | 1--12 | Name
Email = MN | 13-14 | Name
Upvotes: 2