str.find for multiple strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart Nessux

    str.find for multiple strings

    x = str.find(temp, '120.50')

    I am looking for '120.50' '120.51' '122.78' etc. How can I do this with
    just one str.find... I can use re if I must, but I'd like to avoid it if
    possible.

  • Peter Hansen

    #2
    Re: str.find for multiple strings

    Bart Nessux wrote:[color=blue]
    >
    > x = str.find(temp, '120.50')
    >
    > I am looking for '120.50' '120.51' '122.78' etc. How can I do this with
    > just one str.find... I can use re if I must, but I'd like to avoid it if
    > possible.[/color]

    In addition to Fecundo's questions, here's another. What does "temp"
    contain? A single temperature string, or a temperature embedded in
    a bunch of other stuff, or a whole series of temperatures, or what?

    -Peter

    Comment

    • Bart Nessux

      #3
      Re: str.find for multiple strings

      Peter Hansen wrote:[color=blue]
      > Bart Nessux wrote:
      >[color=green]
      >>x = str.find(temp, '120.50')
      >>
      >>I am looking for '120.50' '120.51' '122.78' etc. How can I do this with
      >>just one str.find... I can use re if I must, but I'd like to avoid it if
      >>possible.[/color]
      >
      >
      > In addition to Fecundo's questions, here's another. What does "temp"
      > contain? A single temperature string, or a temperature embedded in
      > a bunch of other stuff, or a whole series of temperatures, or what?
      >
      > -Peter[/color]

      Here's what I'm doing

      def exclude():
      import os
      os.chdir('/home/rbt/scripts')
      inputFile = file('ath_ips.t xt', 'r')
      data = inputFile.read( )
      inputFile.close ()
      comment = '#'
      net0 = '128.173.120.'
      net1 = '128.173.122.'
      host0 = ['50','51','52', '53','54','55']
      host1 = ['17','25','49', '50','55','58', '70']
      for h0 in host0:
      h0 = net0+h0
      rep0 = comment+h0
      sea0 = str.find(data, h0)
      if sea0 >=0:
      data = data.replace(h0 , rep0)
      for h1 in host1:
      h1 = net1+h1
      rep1 = comment+h1
      sea1 = str.find(data, h1)
      if sea1 >=0:
      data = data.replace(h1 , rep1)
      outputFile = file('ath_ips.t xt', 'w')
      outputFile.writ e(data)
      outputFile.clos e()


      Comment

      • Peter Hansen

        #4
        Re: str.find for multiple strings

        Bart Nessux wrote:[color=blue]
        >
        > Peter Hansen wrote:[color=green]
        > > Bart Nessux wrote:
        > >[color=darkred]
        > >>x = str.find(temp, '120.50')
        > >>
        > >>I am looking for '120.50' '120.51' '122.78' etc. How can I do this with
        > >>just one str.find... I can use re if I must, but I'd like to avoid it if
        > >>possible.[/color]
        > >
        > > In addition to Fecundo's questions, here's another. What does "temp"
        > > contain? A single temperature string, or a temperature embedded in
        > > a bunch of other stuff, or a whole series of temperatures, or what?
        > >
        > > -Peter[/color]
        >
        > Here's what I'm doing
        >
        > inputFile = file('ath_ips.t xt', 'r')
        > data = inputFile.read( )[/color]
        [snip]

        Well, since that shows nothing whatever about the content of the
        "data" string (i.e. what is in that ath_ips.txt file), it doesn't
        really help to answer the questions I had.

        -Peter

        Comment

        • Dave K

          #5
          Re: str.find for multiple strings

          On Wed, 11 Feb 2004 14:48:22 -0500 in comp.lang.pytho n, Bart Nessux
          <bart_nessux@ho tmail.com> wrote:
          [color=blue]
          >Peter Hansen wrote:[color=green]
          >> Bart Nessux wrote:
          >>[color=darkred]
          >>>x = str.find(temp, '120.50')
          >>>
          >>>I am looking for '120.50' '120.51' '122.78' etc. How can I do this with
          >>>just one str.find... I can use re if I must, but I'd like to avoid it if
          >>>possible.[/color]
          >>
          >>
          >> In addition to Fecundo's questions, here's another. What does "temp"
          >> contain? A single temperature string, or a temperature embedded in
          >> a bunch of other stuff, or a whole series of temperatures, or what?
          >>
          >> -Peter[/color]
          >
          >Here's what I'm doing
          >
          >def exclude():
          > import os
          > os.chdir('/home/rbt/scripts')
          > inputFile = file('ath_ips.t xt', 'r')
          > data = inputFile.read( )
          > inputFile.close ()
          > comment = '#'
          > net0 = '128.173.120.'
          > net1 = '128.173.122.'
          > host0 = ['50','51','52', '53','54','55']
          > host1 = ['17','25','49', '50','55','58', '70']
          > for h0 in host0:
          > h0 = net0+h0
          > rep0 = comment+h0
          > sea0 = str.find(data, h0)
          > if sea0 >=0:
          > data = data.replace(h0 , rep0)
          > for h1 in host1:
          > h1 = net1+h1
          > rep1 = comment+h1
          > sea1 = str.find(data, h1)
          > if sea1 >=0:
          > data = data.replace(h1 , rep1)
          > outputFile = file('ath_ips.t xt', 'w')
          > outputFile.writ e(data)
          > outputFile.clos e()
          >[/color]

          There's no need to do an explicit find() before replace(). Your 'for'
          loops can be written as

          for h0 in host0:
          data = data.replace(ne t0+h0, comment+net0+h0 )
          for h1 in host1:
          data = data.replace(ne t1+h1, comment+net1+h1 )


          Or reduce it to one 'for' loop with list_comprehens ions

          for host in [net0+h0 for h0 in host0] + [net1+h1 for h1 in host1]:
          data = data.replace(ho st, comment+host)

          Dave

          Comment

          • Bart Nessux

            #6
            Re: str.find for multiple strings

            Dave K wrote:[color=blue]
            > On Wed, 11 Feb 2004 14:48:22 -0500 in comp.lang.pytho n, Bart Nessux
            > <bart_nessux@ho tmail.com> wrote:
            >
            >[color=green]
            >>Peter Hansen wrote:
            >>[color=darkred]
            >>>Bart Nessux wrote:
            >>>
            >>>
            >>>>x = str.find(temp, '120.50')
            >>>>
            >>>>I am looking for '120.50' '120.51' '122.78' etc. How can I do this with
            >>>>just one str.find... I can use re if I must, but I'd like to avoid it if
            >>>>possible.
            >>>
            >>>
            >>>In addition to Fecundo's questions, here's another. What does "temp"
            >>>contain? A single temperature string, or a temperature embedded in
            >>>a bunch of other stuff, or a whole series of temperatures, or what?
            >>>
            >>>-Peter[/color]
            >>
            >>Here's what I'm doing
            >>
            >>def exclude():
            >> import os
            >> os.chdir('/home/rbt/scripts')
            >> inputFile = file('ath_ips.t xt', 'r')
            >> data = inputFile.read( )
            >> inputFile.close ()
            >> comment = '#'
            >> net0 = '128.173.120.'
            >> net1 = '128.173.122.'
            >> host0 = ['50','51','52', '53','54','55']
            >> host1 = ['17','25','49', '50','55','58', '70']
            >> for h0 in host0:
            >> h0 = net0+h0
            >> rep0 = comment+h0
            >> sea0 = str.find(data, h0)
            >> if sea0 >=0:
            >> data = data.replace(h0 , rep0)
            >> for h1 in host1:
            >> h1 = net1+h1
            >> rep1 = comment+h1
            >> sea1 = str.find(data, h1)
            >> if sea1 >=0:
            >> data = data.replace(h1 , rep1)
            >> outputFile = file('ath_ips.t xt', 'w')
            >> outputFile.writ e(data)
            >> outputFile.clos e()
            >>[/color]
            >
            >
            > There's no need to do an explicit find() before replace(). Your 'for'
            > loops can be written as
            >
            > for h0 in host0:
            > data = data.replace(ne t0+h0, comment+net0+h0 )
            > for h1 in host1:
            > data = data.replace(ne t1+h1, comment+net1+h1 )
            >
            >
            > Or reduce it to one 'for' loop with list_comprehens ions
            >
            > for host in [net0+h0 for h0 in host0] + [net1+h1 for h1 in host1]:
            > data = data.replace(ho st, comment+host)
            >
            > Dave[/color]

            Thanks Dave. That's very helpful.

            Comment

            • Josef Meile

              #7
              Re: str.find for multiple strings

              Hi,

              looking at your code is obvious that what you want to do
              is to look for servers on the "ath_ips.tx t" file and then
              comment the line where they appear. I think this search
              and replace function can be done with regular expressions
              (see the re module). The servers belong to the same
              network, and only the two left digits are different.

              Regards,
              Josef


              "Bart Nessux" <bart_nessux@ho tmail.com> wrote in message
              news:c0e0u6$p8g $1@solaris.cc.v t.edu...[color=blue]
              > Peter Hansen wrote:[color=green]
              > > Bart Nessux wrote:
              > >[color=darkred]
              > >>x = str.find(temp, '120.50')
              > >>
              > >>I am looking for '120.50' '120.51' '122.78' etc. How can I do this with
              > >>just one str.find... I can use re if I must, but I'd like to avoid it if
              > >>possible.[/color]
              > >
              > >
              > > In addition to Fecundo's questions, here's another. What does "temp"
              > > contain? A single temperature string, or a temperature embedded in
              > > a bunch of other stuff, or a whole series of temperatures, or what?
              > >
              > > -Peter[/color]
              >
              > Here's what I'm doing
              >
              > def exclude():
              > import os
              > os.chdir('/home/rbt/scripts')
              > inputFile = file('ath_ips.t xt', 'r')
              > data = inputFile.read( )
              > inputFile.close ()
              > comment = '#'
              > net0 = '128.173.120.'
              > net1 = '128.173.122.'
              > host0 = ['50','51','52', '53','54','55']
              > host1 = ['17','25','49', '50','55','58', '70']
              > for h0 in host0:
              > h0 = net0+h0
              > rep0 = comment+h0
              > sea0 = str.find(data, h0)
              > if sea0 >=0:
              > data = data.replace(h0 , rep0)
              > for h1 in host1:
              > h1 = net1+h1
              > rep1 = comment+h1
              > sea1 = str.find(data, h1)
              > if sea1 >=0:
              > data = data.replace(h1 , rep1)
              > outputFile = file('ath_ips.t xt', 'w')
              > outputFile.writ e(data)
              > outputFile.clos e()[/color]


              Comment

              • Peter Hansen

                #8
                Re: str.find for multiple strings

                Peter Hansen wrote:[color=blue]
                >
                > Bart Nessux wrote:[color=green]
                > >
                > > Peter Hansen wrote:[color=darkred]
                > > > Bart Nessux wrote:
                > > >
                > > >>x = str.find(temp, '120.50')
                > > >>
                > > >>I am looking for '120.50' '120.51' '122.78' etc. How can I do this with
                > > >>just one str.find... I can use re if I must, but I'd like to avoid it if
                > > >>possible.
                > > >
                > > > In addition to Fecundo's questions, here's another. What does "temp"
                > > > contain? A single temperature string, or a temperature embedded in
                > > > a bunch of other stuff, or a whole series of temperatures, or what?[/color][/color][/color]

                Ah, trust me to take an industrial-control point of view and not a network
                software point of view. You meant "temp" to be a temporary variable, not
                a temperature, and the 120.50, 120.51 stuff are partial IP addresses, not
                temperatures... how curious of me to confuse them. :-)

                -Peter

                Comment

                Working...