nquestion nquestion - 1 year ago 74
Python Question

Split python object into two objects

I have an object that looks like

object = [{'number':'230/600','blah':'hi'},{'number':'240/620','blah':'hey'}]

` which I want to split into the following format

[{'first':'230','second':'600', 'blah':'hi'},{'first':'240','second':'620','blah':'hey'}]

Any tips on how to do that split based off of what comes before and after the

Answer Source

Assuming the format is fixed, you can do this:

>>> object = [{'number':'230/600'},{'number':'240/620'}]

>>> for value in object:
...     numbers = value['number'].split('/')
...     new_object.append({'numbera': numbers[0], 'numberb': numbers[1]})

>>> new_object
[{'numberb': '600', 'numbera': '230'}, {'numberb': '620', 'numbera': '240'}]

Edit: This was before the question was edited. Mike's answer works great for the edited question.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download