Generate special list from file in python -
i have file :
one:two:three four:five:six seven:height:nine
and on... want parse correctly obtain kind of variable:
myvar = [("one", "two", "three"), ("four", "five", "six"), ("seven", "height", "nine")]
of course, file isn't stopping @ nine, there's lot more of lines after that.
how can in python ?
thanks !
use list compehension:
with open('filename') f: myvar = [line.rstrip().split(':') line in f]
if need list tuples pass line.rstrip().split(':')
tuple()
:
tuple(line.rstrip().split(':'))
Comments
Post a Comment