spliting on ":"

Kent Johnson kent at kentsjohnson.com
Sat Mar 4 12:34:06 EST 2006


s99999999s2003 at yahoo.com wrote:
> hi
> 
> i have a file with
> 
> xxx.xxx.xxx.xxx:yyy
> xxx.xxx.xxx.xxx:yyy
> xxx.xxx.xxx.xxx:yyy
> xxx.xxx.xxx.xxx
> xxx.xxx.xxx.xxx
> xxx.xxx.xxx.xxx:yyy
> 
> i wanna split on ":" and get all the "yyy" and print the whole line out
> so i did
> 
> print line.split(":")[-1]
> 
> but line 4 and 5 are not printed as there is no ":" to split. It should
> print a "blank" at least.
> how to print out lines 4 and 5 ?
> eg output is
> 
> yyy
> yyy
> yyy
> 
> 
> yyy

That's not what I get:

In [2]: data='''xxx.xxx.xxx.xxx:yyy
    ...: xxx.xxx.xxx.xxx:yyy
    ...: xxx.xxx.xxx.xxx:yyy
    ...: xxx.xxx.xxx.xxx
    ...: xxx.xxx.xxx.xxx
    ...: xxx.xxx.xxx.xxx:yyy'''.splitlines()

In [3]: for line in data:
    ...:     print line.split(':')[-1]
    ...:
    ...:
yyy
yyy
yyy
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx
yyy

Kent



More information about the Python-list mailing list