str.find for multiple strings

Discussion in 'Python' started by Bart Nessux, Feb 11, 2004.

  1. Bart Nessux

    Bart Nessux Guest

    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.
    Bart Nessux, Feb 11, 2004
    #1
    1. Advertising

  2. Bart Nessux

    Peter Hansen Guest

    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
    Peter Hansen, Feb 11, 2004
    #2
    1. Advertising

  3. Bart Nessux

    Bart Nessux Guest

    Peter Hansen wrote:
    > 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


    Here's what I'm doing

    def exclude():
    import os
    os.chdir('/home/rbt/scripts')
    inputFile = file('ath_ips.txt', '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.txt', 'w')
    outputFile.write(data)
    outputFile.close()
    Bart Nessux, Feb 11, 2004
    #3
  4. Bart Nessux

    Peter Hansen Guest

    Bart Nessux wrote:
    >
    > Peter Hansen wrote:
    > > 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

    >
    > Here's what I'm doing
    >
    > inputFile = file('ath_ips.txt', 'r')
    > data = inputFile.read()

    [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
    Peter Hansen, Feb 11, 2004
    #4
  5. Bart Nessux

    Dave K Guest

    On Wed, 11 Feb 2004 14:48:22 -0500 in comp.lang.python, Bart Nessux
    <> wrote:

    >Peter Hansen wrote:
    >> 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

    >
    >Here's what I'm doing
    >
    >def exclude():
    > import os
    > os.chdir('/home/rbt/scripts')
    > inputFile = file('ath_ips.txt', '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.txt', 'w')
    > outputFile.write(data)
    > outputFile.close()
    >


    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(net0+h0, comment+net0+h0)
    for h1 in host1:
    data = data.replace(net1+h1, comment+net1+h1)


    Or reduce it to one 'for' loop with list_comprehensions

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

    Dave
    Dave K, Feb 12, 2004
    #5
  6. Bart Nessux

    Bart Nessux Guest

    Dave K wrote:
    > On Wed, 11 Feb 2004 14:48:22 -0500 in comp.lang.python, Bart Nessux
    > <> wrote:
    >
    >
    >>Peter Hansen wrote:
    >>
    >>>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

    >>
    >>Here's what I'm doing
    >>
    >>def exclude():
    >> import os
    >> os.chdir('/home/rbt/scripts')
    >> inputFile = file('ath_ips.txt', '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.txt', 'w')
    >> outputFile.write(data)
    >> outputFile.close()
    >>

    >
    >
    > 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(net0+h0, comment+net0+h0)
    > for h1 in host1:
    > data = data.replace(net1+h1, comment+net1+h1)
    >
    >
    > Or reduce it to one 'for' loop with list_comprehensions
    >
    > for host in [net0+h0 for h0 in host0] + [net1+h1 for h1 in host1]:
    > data = data.replace(host, comment+host)
    >
    > Dave


    Thanks Dave. That's very helpful.
    Bart Nessux, Feb 12, 2004
    #6
  7. Bart Nessux

    Josef Meile Guest

    Hi,

    looking at your code is obvious that what you want to do
    is to look for servers on the "ath_ips.txt" 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" <> wrote in message
    news:c0e0u6$p8g$...
    > Peter Hansen wrote:
    > > 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

    >
    > Here's what I'm doing
    >
    > def exclude():
    > import os
    > os.chdir('/home/rbt/scripts')
    > inputFile = file('ath_ips.txt', '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.txt', 'w')
    > outputFile.write(data)
    > outputFile.close()
    Josef Meile, Feb 12, 2004
    #7
  8. Bart Nessux

    Peter Hansen Guest

    Peter Hansen wrote:
    >
    > Bart Nessux wrote:
    > >
    > > Peter Hansen wrote:
    > > > 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?


    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
    Peter Hansen, Feb 12, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    477
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    629
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    337
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,701
    Oliver Wong
    Aug 3, 2006
  5. Giacomo Alzetta
    Replies:
    10
    Views:
    240
    Giacomo Alzetta
    Nov 22, 2012
Loading...

Share This Page