Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help finding a string in Python...
#1
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... :/
GamerCraft

Visit it, and tell me what you think. Tongue
Reply
#2
(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
Do what thou wilt shall be the whole of the Law. Love is the law, love under will.
.::The Rights of Man::.
Reply
#3
(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?
[Image: izsyo6.jpg]


Reply
#4
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.
GamerCraft

Visit it, and tell me what you think. Tongue
Reply
#5
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.
[Image: izsyo6.jpg]


Reply
#6
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.
GamerCraft

Visit it, and tell me what you think. Tongue
Reply
#7
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.
[Image: izsyo6.jpg]


Reply
#8
Hmm. Java. D:

Well, thanks. I'll look into Java I guess.
GamerCraft

Visit it, and tell me what you think. Tongue
Reply
#9
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
Reply
#10
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.
[Image: sig.php]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Help Dεlluzion 3 1,793 09-30-2019, 12:59 AM
Last Post: samsmith001
  Simple Python Python Compiler Canoris 21 8,441 06-01-2011, 06:30 PM
Last Post: Filefinder
  Python 2 vs 3? Jake 8 2,256 12-11-2010, 04:13 PM
Last Post: Bursihido
  Python help Kharnage 2 763 02-12-2010, 09:07 PM
Last Post: Kharnage
  "==" and "is" in Python Canoris 1 752 02-07-2010, 03:55 PM
Last Post: uber1337

Forum Jump:


Users browsing this thread: 1 Guest(s)