spliting on ":"

Peter Hansen peter at engcorp.com
Sat Mar 4 22:33:24 CET 2006

Cyril Bazin wrote:
> Your file looks like a list of IP adresses.
> You can use the urllib and urllib2 modules to parse IP adresses.
> import urllib2
> for line in open("fileName.txt"):
>     addr, port  = urllib2.splitport(line)
>     print (port != None) and '' or port

Is this what you want to happen when port is None?

 >>> port = None
 >>> print (port != None) and '' or port

I think you're getting caught by the classic and/or trap in Python, 
trying to avoid using a simple if statement.

By the way, equality comparison with None is generally a bad idea as 
well, so even if the above worked it should be (port is not None) rather 
than (port != None).


More information about the Python-list mailing list