2

I have a file, full of data in a certain format, I want to fill my own data structure with that data

for example, I can have a file like this:

John - Smith : 0123
children: 
  Sam
  Kim

I want to do something with that string, in order to extract the data into for example

firstName = "John"
lastName = "Smith"
number = "0123"
children = ['Sam', 'Kim']

I hope there's an easier way than using separators..

Mohamed Khamis
  • 7,731
  • 10
  • 38
  • 58

1 Answers1

4

Here is a regex solution:

>>> import re
>>> data = 'John - Smith : 0123\nchildren: \n  Sam\n  Kim'
>>> match = re.match(r'(\w+) - (\w+) : (\d+).*?children:(.*)', data, re.S)
>>> match.groups()
('John', 'Smith', '0123', ' \n  Sam\n  Kim')

You can then assign the groups to your variables:

>>> firstName, lastName, number = match.groups()[:3]
>>> children = [c.strip() for c in match.group(4).strip().split('\n')]

and the result...

>>> firstName
'John'
>>> lastName
'Smith'
>>> number
'0123'
>>> children
['Sam', 'Kim']
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306