Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > str.find for multiple strings

Reply
Thread Tools

str.find for multiple strings

 
 
Bart Nessux
Guest
Posts: n/a
 
      02-11-2004
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.

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      02-11-2004
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
 
Reply With Quote
 
 
 
 
Bart Nessux
Guest
Posts: n/a
 
      02-11-2004
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()


 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      02-11-2004
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
 
Reply With Quote
 
Dave K
Guest
Posts: n/a
 
      02-12-2004
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
 
Reply With Quote
 
Bart Nessux
Guest
Posts: n/a
 
      02-12-2004
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.

 
Reply With Quote
 
Josef Meile
Guest
Posts: n/a
 
      02-12-2004
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()



 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      02-12-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57