#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    2
    Rep Power
    0

    String Manipulation - please help make more concise


    I have a multi line string and I need to pull specific pieces of data from it.
    Code:
    I389
    Date Field
    10/02/05
    ;SECTION2
    Test Data
    Not Formatted
    ;SECTION3
    Numerical Values
    39102
    42244
    16723
    23414
    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:
    Code:
            answerSplit = StringObject.split(';SECTION2')
            answerString = str(answerSplit[1])
            answer = answerString.split(';SECTION3')
            FileWrite(str(answer[0]).rstrip())
    1. Split StringObject into a list with 2 values (at ;SECTION2)
    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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    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.
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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:

    python Code:
    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[1]) since answerSplit[1] is already a string.

    Dave

    Comments on this post

    • Yegg` agrees
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    2
    Rep Power
    0

    Thumbs up Thanks!


    Everyone,

    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    I would probably go for something like:

    Code:
    tmp = """I389
            Date Field
            10/02/05
            ;SECTION2
            Test Data
            Not Formatted
            ;SECTION3
            Numerical Values
            39102
            42244
            16723
            23414"""
    
    section2start = tmp.find(";SECTION2")
    section2end = section2start + len(";SECTION2")
    
    section3start = tmp.find(";SECTION3")
    
    section2 = tmp[section2end:section3start]
    Which you could reduce to:

    Code:
    tmp = myString
    section2 = tmp[tmp.find("SECTION2")+8:tmp.find(";SECTION3")]
    But it's a bit less helpful to read...

IMN logo majestic logo threadwatch logo seochat tools logo