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..

1 Answer 1

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']
Sign up to request clarification or add additional context in comments.

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.