spliting on ":"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s99999999s2003@yahoo.com

    #1

    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

  • Duncan Booth

    #2
    Re: spliting on ":&quot ;

    s99999999s2003@ yahoo.com wrote:
    [color=blue]
    > 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[/color]

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

    what's the problem?

    Comment

    • Kent Johnson

      #3
      Re: spliting on ":&quot ;

      s99999999s2003@ yahoo.com wrote:[color=blue]
      > 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[/color]

      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'''.splitli nes()

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

      Kent

      Comment

      • Peter Hansen

        #4
        Re: spliting on ":&quot ;

        Cyril Bazin wrote:[color=blue]
        > 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.splitpo rt(line)
        > print (port != None) and '' or port[/color]

        Is this what you want to happen when port is None?
        [color=blue][color=green][color=darkred]
        >>> port = None
        >>> print (port != None) and '' or port[/color][/color][/color]
        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

        Comment

        • Petr Jakes

          #5
          Re: spliting on ":&quot ;

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

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

          Petr Jakes

          Comment

          • Peter Hansen

            #6
            Re: spliting on ":&quot ;

            Cyril Bazin wrote:[color=blue]
            > 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 "==".[/color]

            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).
            [color=blue]
            > The correct program is:
            >
            > import urllib2
            > for line in open("fileName. txt"):
            > addr, port = urllib2.splitpo rt (line)
            > print (port == None) and '' or port[/color]

            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:
            [color=blue][color=green][color=darkred]
            >>> port = None
            >>> print (port == None) and '' or port[/color][/color][/color]
            None[color=blue][color=green][color=darkred]
            >>> print (port != None) and '' or port[/color][/color][/color]
            None

            and even:
            [color=blue][color=green][color=darkred]
            >>> print (port is None) and '' or port[/color][/color][/color]
            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.
            [color=blue]
            > or
            >
            > import urllib2
            > for line in open("fileName. txt"):
            > addr, port = urllib2.splitpo rt(line)
            > if port == None:
            > print ''
            > else:
            > print port[/color]

            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

            Comment

            • Peter Hansen

              #7
              Re: spliting on ":&quot ;

              Petr Jakes wrote:[color=blue][color=green]
              >>I think you're getting caught by the classic and/or trap in Python,
              >>trying to avoid using a simple if statement.[/color]
              >
              > What do you mean by "the classic and/or trap"? Can you give an example
              > please?[/color]

              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

              Comment

              • Steven D'Aprano

                #8
                Re: spliting on ":&quot ;

                On Sat, 04 Mar 2006 08:54:33 -0800, s99999999s2003 wrote:
                [color=blue]
                > 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[/color]


                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.

                Comment

                • fileexit

                  #9
                  Re: spliting on ":&quot ;

                  [color=blue]
                  > yyy
                  > yyy
                  > yyy
                  > xxx.xxx.xxx.xxx
                  > xxx.xxx.xxx.xxx[/color]

                  of course you will get this result...

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

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

                  So the result is very very normal

                  Comment

                  • Bryan Olson

                    #10
                    Re: spliting on ":&quot ;

                    Peter Hansen wrote:[color=blue]
                    > 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.[/color]

                    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.

                    [color=blue]
                    > 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).[/color]

                    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

                    Comment

                    • Steve Holden

                      #11
                      Re: spliting on ":&quot ;

                      Bryan Olson wrote:[color=blue]
                      > Peter Hansen wrote:
                      >[color=green]
                      >>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.[/color]
                      >
                      >
                      > 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.
                      >[/color]
                      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.[color=blue]
                      >
                      >[color=green]
                      >>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).[/color]
                      >
                      >
                      > 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".
                      >
                      >[/color]
                      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.blogs pot.com

                      Comment

                      • Peter Hansen

                        #12
                        Re: spliting on ":&quot ;

                        Cyril Bazin wrote:[color=blue]
                        > I agree. None is an object! If you want to compare an object to another
                        > object why not using "=="?[/color]

                        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

                        Comment

                        Working...