473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

spliting on ":"

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

thanks

Mar 4 '06 #1
11 1335
s9************@yahoo.com wrote:
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


if ":" in line:
print line.split(":")[-1]
else:
print

what's the problem?
Mar 4 '06 #2
s9************@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
Mar 4 '06 #3
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

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


What do you mean by "the classic and/or trap"? Can you give an example
please?

Petr Jakes

Mar 4 '06 #5
Cyril Bazin wrote:
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 archives could tell you more, but basically on is usually interested
in *identity* with a singleton object (None), not in whether the object
on is examining happens to compare equal. A custom object could be
designed to compare equal to None in certain cases, even though it *is
not* None, leading to the "== None" approach being defective code.

In the specific code in question, it won't make any differences, but I
pointed it out to help folks who don't know this to start developing the
safer habit, which is always to use "is" and "is not" with None (or,
generally, with other singletons).
The correct program is:

import urllib2
for line in open("fileName.txt"):
addr, port = urllib2.splitport (line)
print (port == None) and '' or port
Nope, sorry... the point is that the "and/or" pseudo-ternary operator is
inherently flawed if the operand after "and" evaluates False. Check
this out:
port = None
print (port == None) and '' or port None print (port != None) and '' or port None

and even:
print (port is None) and '' or port

None

So the bug isn't in using "!=" instead of "==", or using equality
instead of identity comparison, it is in trying to use the and/or
expression for a purpose it wasn't intended for.
or

import urllib2
for line in open("fileName.txt"):
addr, port = urllib2.splitport(line)
if port == None:
print ''
else:
print port


That should work nicely... and it's more readable too!

Note that in Python 2.5 you should be able to write this as

print '' if port is None else port
or print '' if (port is None) else port

but it's quite arguable whether that is better than the simple if/else
statement in this case.

-Peter

Mar 5 '06 #6
Petr Jakes wrote:
I think you're getting caught by the classic and/or trap in Python,
trying to avoid using a simple if statement.


What do you mean by "the classic and/or trap"? Can you give an example
please?


Sure, see my subsequent reply to Cyril, elsewhere in this thread.

(In summary, and/or is defective when used as a ternary operator if the
expression after "and" evaluates False. The list archives can tell you
more.)

-Peter

Mar 5 '06 #7
On Sat, 04 Mar 2006 08:54:33 -0800, s99999999s2003 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

There are a few ways of handling this problem. It depends on what you want
to do if your input string looks like this "abc:y:z": should your code
print "y", "z" or even "y:z"?

# look before you leap
line = "abc:y:z"
if ":" in line:
print line.split(":")[-1] # prints "z"
print line.split(":")[1] # prints "y"
print line.split(":", 1)[-1] # prints "y:z"
# if your line only has one colon, the above three lines
# should all print the same thing
else:
print ""

# look before you leap again
L = line.split(":")
if len(L) > 1:
print L[1]
else:
print ""

# use a sentinel value
print (line + ":").split(":", 1)[1][:-1]
--
Steven.

Mar 5 '06 #8
yyy
yyy
yyy
xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx


of course you will get this result...

inside the loop, when line="xxx.xxx.xxx.xxx:yyy"
line.split(":") will give a list ["xxx.xxx.xxx.xxx", "yyy"], and
element -1 will be "yyy"

but when line="xxx.xxx.xxx.xxx"
line.split(":") will give a list ["xxx.xxx.xxx.xxx"], and element -1
will be "xxx.xxx.xxx.xxx"

So the result is very very normal

Mar 6 '06 #9
Peter Hansen wrote:
The archives could tell you more, but basically on is usually interested
in *identity* with a singleton object (None), not in whether the object
on is examining happens to compare equal. A custom object could be
designed to compare equal to None in certain cases, even though it *is
not* None, leading to the "== None" approach being defective code.
But if a custom class allows instances to compare as equal to None,
we might reasonably expect the programmers had a reason. There's not
much anyone can do with None besides passing it around and comparing
it by value or identity. Insisting on 'is' rather than '==' will break
whatever polymorphism such a custom object was trying to achieve.

In the specific code in question, it won't make any differences, but I
pointed it out to help folks who don't know this to start developing the
safer habit, which is always to use "is" and "is not" with None (or,
generally, with other singletons).


Hmmm... To make my code safer, I'm thinking I should replace doc strings
that say "if bluf is Null" with "if blurf compares equal to Null".
--
--Bryan

Mar 6 '06 #10
Bryan Olson wrote:
Peter Hansen wrote:
The archives could tell you more, but basically on is usually interested
in *identity* with a singleton object (None), not in whether the object
on is examining happens to compare equal. A custom object could be
designed to compare equal to None in certain cases, even though it *is
not* None, leading to the "== None" approach being defective code.

But if a custom class allows instances to compare as equal to None,
we might reasonably expect the programmers had a reason. There's not
much anyone can do with None besides passing it around and comparing
it by value or identity. Insisting on 'is' rather than '==' will break
whatever polymorphism such a custom object was trying to achieve.

That's all very well, but completely hypothetical. What's the use case
for this theoretical object? Why should anything that isn't None compare
equal with it?

None is explicitly defined as a type with a single instance, so
comparing equal to None without *being* None seems like a *very* bad idea.
In the specific code in question, it won't make any differences, but I
pointed it out to help folks who don't know this to start developing the
safer habit, which is always to use "is" and "is not" with None (or,
generally, with other singletons).

Hmmm... To make my code safer, I'm thinking I should replace doc strings
that say "if bluf is Null" with "if blurf compares equal to Null".

Humour?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 6 '06 #11
Cyril Bazin wrote:
I agree. None is an object! If you want to compare an object to another
object why not using "=="?


The term "compare" is ambiguous. If you want to compare for *identity*,
you use "is". If you want to compare for equality, you use "==". In
most cases with None you are interested in identity, not equality. It's
that simple.

As I said, the archives have extensive discussions about this with lots
of the nuances already covered in far more detail than I'm going to do
again in this thread. (The advice was free, so you're quite welcome to
consider it to be worth what you paid.)

-Peter

Mar 7 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brad Kent | last post by:
Anyone out there have any tricks or scripts to take some text of unknown length and display it in two (or more) columns of equal height? The text may or may not contain "hard-coded" linebreaks or...
43
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
1
by: David Furey | last post by:
Hi I have an XML documnet and a XSLT document as shown below THe XSLT document brings back a filtered docmument that has the VendorName that starts with a particular sub-string This works as...
3
by: NecroJoe | last post by:
I am using PHP to generate a little javascript for one of my pages. In short it allows a user to select a value from a list and pop it into a form field on a seperate page. This works well unless...
5
by: Mateusz Loskot | last post by:
Hi, I'd like to ask how XML parsers should handle attributes which consists of " entity as value. I know XML allows to use both: single and double quotes as attribute value terminator. That's...
3
by: Arpi Jakab | last post by:
I have a main project that depends on projects A and B. The main project's additional include directories list is: ...\ProjectA\Dist\Include ...\ProjectB\Dist\Include Each of the include...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
8
by: Ulysse | last post by:
Hello, I need to clean the string like this : string = """ bonne mentalit&eacute; mec!:) \n <br>bon pour info moi je suis un serial posteur arceleur dictateur ^^* \n ...
1
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
4
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.