spliting on ":"
Cyril Bazin
cyril.bazin at info.unicaen.fr
Sat Mar 4 17:40:20 EST 2006
Ok, ok, there was a mistake in the code.
(Of course, it was done on prupose in order to verify if everybody is aware
;-)
I don't know why it is preferable to compare an object to the object "None"
using "is not".
"==" is, IMHO, more simple. Simple is better than complex. So I use "==".
The correct program is:
import urllib2
for line in open("fileName.txt"):
addr, port = urllib2.splitport(line)
print (port == None) and '' or port
or
import urllib2
for line in open("fileName.txt"):
addr, port = urllib2.splitport(line)
if port == None:
print ''
else:
print port
On 3/4/06, Peter Hansen <peter at engcorp.com> wrote:
>
> 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
> None
>
>
> 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).
>
> -Peter
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060304/8328d963/attachment.html>
More information about the Python-list
mailing list