473,378 Members | 1,360 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,378 software developers and data experts.

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.

Jul 18 '05 #1
7 4807
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
Jul 18 '05 #2
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()
Jul 18 '05 #3
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
Jul 18 '05 #4
On Wed, 11 Feb 2004 14:48:22 -0500 in comp.lang.python, Bart Nessux
<ba*********@hotmail.com> 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
Jul 18 '05 #5
Dave K wrote:
On Wed, 11 Feb 2004 14:48:22 -0500 in comp.lang.python, Bart Nessux
<ba*********@hotmail.com> 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.

Jul 18 '05 #6
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" <ba*********@hotmail.com> wrote in message
news:c0**********@solaris.cc.vt.edu...
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()

Jul 18 '05 #7
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
Jul 18 '05 #8

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

Similar topics

3
by: hokiegal99 | last post by:
How do I say: x = string.find(files, 'this', 'that', 'the-other') currently I have to write it like this to make it work: x = string.find(files, 'this') y = string.find(files, 'that') z =...
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
4
by: Anthony | last post by:
Hello, I am writing a function that populates an array of pointers to strings. Both the number of strings in the array, and the lengths of the strings, are dynamic; in particular, the number of...
3
by: 00_CuMPe3WaR3D12 | last post by:
I know there is Culture feature with Asp.net, but what I want to know is how to organize/design a web application that supports multiple languages. What is the best approach? Do I use "If,...
19
by: santosh | last post by:
Hi all, In the following program I allocate a block of pointers to type char, initialised to zero. I then point each of those pointers to a block of allocated memory of fixed size (33 bytes). A...
5
by: Buddhist[CHinA] | last post by:
The text files are not only the .txt files, but also all ascii files. Thx.
3
by: computerwolf8 | last post by:
I have a file where I know the lines go as follows: string long string int int string double
2
by: rengask | last post by:
I got the code to find and replace within an open text file. ------------------ Private Sub cmdFile_Click() Dim strTemp As String txtFile = "" dlg.FileName = "*.*" dlg.ShowOpen ...
5
by: neeludhiman | last post by:
Hi All, Can someone please help me with the code in C / C++ to find a string in an input text file and replace it with another string in output text file. The catch is that white spaces in the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.