November 14th, 2005, 04:24 PM
String Manipulation - please help make more concise
I have a multi line string and I need to pull specific pieces of data from it.
What I'd like is to grab the data in between ;SECTION2 and ;SECTION3. The line numbers vary. Sometimes ;SECTION2 begins on line 2 sometimes 8 or anywhere in between. What I've done is the following:
1. Split StringObject into a list with 2 values (at ;SECTION2)
answerSplit = StringObject.split(';SECTION2')
answerString = str(answerSplit)
answer = answerString.split(';SECTION3')
2. Set answerString to the second value in the list (which is: Test Data\n through 23414)
3. Split answerString into 2 values (at ;SECTION3)
4. send the results string (data between ;SECTION2 and ;SECTION3) to my FileWrite function
I'm curious to know if there's a more efficient way of doing this. Perhaps the use of re would be better here?
November 14th, 2005, 04:58 PM
well yes you could use a re expresion but that would be over doing it alittle. I would probabyl use a for loop to interate over the sequence of items after they were split and put them in a new list...
Those people who think they know everything are a great annoyance to those of us who do.
November 14th, 2005, 07:36 PM
Here's an easy way. Loop through all of those pieces of data, in the item is equal to ';SECTION2' or begins with ';', however you want it, then you can then after the loop iterates to the next item, you can first check to see if the next item is equal to ';SECTION3' or again starts with a ';'. If the item is not equal to ';SECTION3', then add the item to a "special list". This special list will just hold the items that you want to collect, so you will continue to add items after ';SECTION2' has been found, until ';SECTION3' is found. Once ';SECTION3' is found, your loop will end because hopefully you remember to add in a break statement if item == ';SECTION3' is true. Hope this helps.
November 15th, 2005, 03:37 AM
If you know that there will always be one of each ;SECTION marker then your original approach of using split is probably the simplest and most efficient way of doing it. You can give a second parameter to split to tell it how many times to split the string, which will make it slightly more efficient.
I would code it like this:
section1, remainder = StringObject.split(';SECTION2', 1)
section2, section3 = remainder.split(';SECTION3', 1)
since you know that string.split('XXX', 1) will always return a list of two strings as long as 'XXX' is in the original string, you can assign the result directly to two variables.
BTW, in your original code there is no need to use str(answerSplit) since answerSplit is already a string.
Comments on this post
November 15th, 2005, 09:56 AM
Thanks for the advice.
Dave, thanks for that tip. I'm not a programmer by trade, so I like to verify I'm learning correctly.
November 15th, 2005, 12:37 PM
I would probably go for something like:
Which you could reduce to:
tmp = """I389
section2start = tmp.find(";SECTION2")
section2end = section2start + len(";SECTION2")
section3start = tmp.find(";SECTION3")
section2 = tmp[section2end:section3start]
But it's a bit less helpful to read...
tmp = myString
section2 = tmp[tmp.find("SECTION2")+8:tmp.find(";SECTION3")]