Support Forums

Full Version: Need help finding a string in Python...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to find certain keywords in Python, and insert all of the contents between the starting and ending keywords into the first declaration of the specific block.

How would I do this? I've been Googling, and can't find anything about it... :/
(02-16-2010, 03:30 PM)TheLifelessOne Wrote: [ -> ]I'm trying to find certain keywords in Python, and insert all of the contents between the starting and ending keywords into the first declaration of the specific block.

How would I do this? I've been Googling, and can't find anything about it... :/
Actually, I've had the same problem, the answer would be much appreciated no only by the latter member... Oui
(02-16-2010, 03:30 PM)TheLifelessOne Wrote: [ -> ]into the first declaration of the specific block.
I'm having trouble understanding this part, do you want to insert words between one word and another in a string?
It's simple. It searches for a specific string inside of a text file (or variable, whatever). When it finds the first "instance" of the string, it logs it's location, and the location of the ending string, which creates the block (think of a block like a C function. It has a beginning, an end, and hold stuff).
After it does that, it searches for more blocks, and if it finds them, merges them with the first one.
Hmmm like this:
Code:
>>> key = 'if'
>>> string = 'if this then if'
>>> list = string.split(key)
>>> chunk = list[1:2]
>>> chunk
[' this then ']
You want everything in between an indicative word, right?
If you want to take everything between two indicative statements (if, end), it would look like this:
Code:
>>> x = 'if bla bla bla lol end'
>>> y = 'if end'
>>> chunk = x.strip(y)
>>> chunk
'bla bla bla lol'
If this is what you are looking from I may be to write a script that will achieve what you want.
I think the second one is the one I want.

Here's an example of what it'll do, if that'll help clear it up.

Code:
startblock // first instance. It will save the position of this.
    "hello world!"
    "hai thar!"
    "wait, what?"
endblock

startblock // second instance.
   "well, now this is interesting."
endblock

// The second block now merges with the first, so:

startblock
    "hello world!"
    "hai thar!"
    "wait, what?"
    "well, now this is interesting."
endblock // this is what it becomes.
Wow this is harder than I thought and I don't really have the time to figure it out but I can try to help you. As for finding positions, take a look at this. For text scanning/replacing of this depth I would suggest looking into Java's scanner class, there probably is a python way I'm just not there yet, sorry Sad.
Hmm. Java. D:

Well, thanks. I'll look into Java I guess.
Hmm. Maybe something like this:

Code:
>>> x = ["Hello","There"]
>>> y = ["Wait","What"]
>>> a = " ".join(x)
>>> b = " ".join(y)
>>> c = a,b
>>> d = " ".join(c)
>>> print d
Hello There Wait What
you're trying to overcomplicate matters, the reason you cant find a solution is because you aren't using any python terminology. the only thing i can make from any of your posts is that you want a list of strings, and when it finds the string you want just append that string to the list. I cant make out how you want the strings to be chosen but simplying reading a file and if not file.find(string) == -1: while your not to the end of your string continue, if you are at the end append the last word to the local string and append the local string to the list of strings.

what you are saying sounds incredibly easy to do with just string methods, just try and explain what you are trying to do better.