You can assign to different variables. Like in Perl, you just need to define them in an array, so assignation is done by matching position of variable and result.
Here is something I tried in interactive python:
>>> # this is a grep result, btw
... foo = 'config/some.conf:12: title = "Super Me"'
>>> [ filename, line, text ] = foo.split(':')
>>> print text
title = "Super Me"
I do like this rather than a dictionary or an array, especially when working in a for
loop. It makes variable names more meaningful, even if local to the loop, or temporary.
Edit
second edit to integrate codeforester's notes (Thanks).
To avoid searching for variables usage, unwanted values can be dummied to clearly state it will not be used.
Dummy variables are expected as _
by python linter
>>> [ _, line, text ] = foo.split(':')
If you don't need the List properties with your variables, you can just remove the square brackets (variables are then managed as a tuple):
>>> filename, line, text = foo.split(':')
If you are not sure about the tokens quantity, use the extended iterable, which requires a List:
>>> [ filename, line, text, *_ ] = foo.split(':')
End of edit
Readability for the win !
list
? – Louislouisa