PDA

View Full Version : python - locate list items in string



smerny
September 24th, 2009, 02:32 AM
I have a list of a few strings and a big txt file. What would be the best way find the location (or possibly multiple locations) of where the strings in the list exist in the txt file? (Line and position within the line)

ghostdog74
September 24th, 2009, 02:37 AM
show your input files, and describe your output.

ve4cib
September 24th, 2009, 02:54 AM
http://imgs.xkcd.com/comics/regular_expressions.png

If you know what a list looks like (probably enclosed in [] characters, with 1 or more alpha-numeric strings, separated by commas) you can create a regex and apply that to your input file. Should do the trick.

Unless of course I've completely misunderstood your goal.

smerny
September 24th, 2009, 02:59 AM
input should be flexible... output can be something like...


if search[1] = "blue" and the word "blue" and lets say that line 5 of the txt file says "There was a blue crayon." and line 11 says "He drew a blue ball."

the output should be:


The word "blue" shows up on:
- line 5 on the 13th character
- line 11 on the 10th character

The word "whatever" shows up on:
- ...etc

ghostdog74
September 24th, 2009, 04:11 AM
so what have you tried on your own? have you read up on Python's basics?

ve4cib
September 24th, 2009, 04:53 AM
There's a convenient function called "find" that's a member of the string class. Chances are you'll be wanting to use that *hint-hint*.



>>> print "".find.__doc__
S.find(sub [,start [,end]]) -> int

Return the lowest index in S where substring sub is found,
such that sub is contained within s[start:end]. Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.

>>> s="The blue ball rolled under the red car"
>>> s.find('blue')
4
>>> s.find('red')
31
>>> s.find('green')
-1

A_Fiachra
September 24th, 2009, 05:34 AM
There's a convenient function called "find" that's a member of the string class. Chances are you'll be wanting to use that *hint-hint*.

..


Oh, sure -- if you want to do it the easy way!!!!


First ... get the source code for egreg, install the boost C++ python libraries, fire up bison and flex and start a pot of coffee.

Now the nontrivial part ....


:P

A_Fiachra
September 24th, 2009, 05:34 AM
s/egreg/egrep/

ve4cib
September 24th, 2009, 06:02 AM
Oh, sure -- if you want to do it the easy way!!!!


First ... get the source code for egreg, install the boost C++ python libraries, fire up bison and flex and start a pot of coffee.

Now the nontrivial part ....


:P

"There should be one (and preferably only one) obvious way of doing something."

Wise words from our Benevolent Dictator for Life...