Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about string find?

Tags:

python

I have a list of data that I want to search through. This new list of data is structured like so.

name, address dob family members age height etc..

I want to search through the lines of data so that I stop the search at the ',' that appears after the name to optimize the search. I believe I want to use this command:

str.find(sub[, start[, end]])

I'm having trouble writing the code in this structure though. Any tips on how to make string find work for me?

Here is some sample data:

Bennet, John, 17054099","5","156323558","-","0", 714 // 
Menendez, Juan,7730126","5","158662525" 11844 // 
Brown,  Jamal,"9","22966592","+","0",,"4432 // 

The idea is I want my program to search only to the first ',' and not search through the rest of the large lines.

EDIT. So here is my code.

I want the to search the lines in completedataset only until the first comma. I'm still confused as to how I should implement these suggestions into my existing code.

counter = 1
 for line in completedataset:
     print counter
     counter +=1
     for t in matchedLines:
         if t in line:
             smallerdataset.write(line)
like image 1000
Robert A. Fettikowski Avatar asked Feb 27 '23 12:02

Robert A. Fettikowski


2 Answers

You can do it quite directly:

s = 'Bennet, John, 17054099","5","156323558","-","0", 714 //'
print s.find('John', 0, s.index(',')) # find the index of ',' and stop there
like image 162
Jochen Ritzel Avatar answered Mar 07 '23 11:03

Jochen Ritzel


If I understand your specs correctly,

for thestring in listdata:
    firstcomma = thestring.find(',')
    havename = thestring.find(name, 0, firstcomma)
    if havename >= 0:
        print "found name:", thestring[:firstcomma]

Edit: given the OP's edit of the Q, this would become something like:

 counter = 1
 for line in completedataset:
     print counter
     counter += 1
     firstcomma = thestring.find(',')
     havename = thestring.find(t, 0, firstcomma)
     if havename >= 0:
         smallerdataset.write(line)

Of course, the use of counter is unPythonically low-level, and a better eqv would be

 for counter, line in enumerate(completedataset):
     print counter + 1
     firstcomma = thestring.find(',')
     havename = thestring.find(t, 0, firstcomma)
     if havename >= 0:
         smallerdataset.write(line)

but that doesn't affect the question as asked.

like image 33
Alex Martelli Avatar answered Mar 07 '23 10:03

Alex Martelli