Splitting or stripping a variable number of characters from a line of text in Python?
Question: I have a large amount of data of this type:

    
    
      array(14) {
        ["ap_id"]=>
        string(5) "22755"
        ["user_id"]=>
        string(4) "8872"
        ["exam_type"]=>
        string(32) "PV Technical Sales Certification"
        ["cert_no"]=>
        string(12) "PVTS081112-2"
        ["explevel"]=>
        string(1) "0"
        ["public_state"]=>
        string(2) "NY"
        ["public_zip"]=>
        string(5) "11790"
        ["email"]=>
        string(19) "ivorabey@zeroeh.com"
        ["full_name"]=>
        string(15) "Ivor Abeysekera"
        ["org_name"]=>
        string(21) "Zero Energy Homes LLC"
        ["org_website"]=>
        string(14) "www.zeroeh.com"
        ["city"]=>
        string(11) "Stony Brook"
        ["state"]=>
        string(2) "NY"
        ["zip"]=>
        string(5) "11790"
      }
    

I wrote a for loop in python which reads through the file, creating a
dictionary for each array and storing elements like thus:

    
    
    a = 0
    data = [{}]
    
    with open( "mess.txt" ) as messy:
            lines = messy.readlines()
            for i in range( 1, len(lines) ):
                line = lines[i]
                if "public_state" in line:
                    data[a]['state'] = lines[i + 1]
                elif "public_zip" in line:
                    data[a]['zip'] = lines[i + 1]
                elif "email" in line:
                    data[a]['email'] = lines[i + 1]
                elif "full_name" in line:
                    data[a]['contact'] = lines[i + 1]
                elif "org_name" in line:
                    data[a]['name'] = lines[i + 1]
                elif "org_website" in line:
                    data[a]['website'] = lines[i + 1]
                elif "city" in line:
                    data[a]['city'] = lines[i + 1]
                elif "}" in line:
                    a += 1
                    data.append({})
    

I know my code is terrible, but I am fairly new to Python. As you can see, the
bulk of my project is complete. What's left is to strip away the code tags
from the actual data. For example, I need `string(15) "Ivor Abeysekera"` to
become `Ivor Abeysekera"`.

After some research, I considered `.lstrip()`, but since the preceding text is
always different.. I got stuck.

Does anyone have a clever way of solving this problem? Cheers!

Edit: I am using Python 2.7 on Windows 7.


Answer: **BAD SOLUTION Based on current question**

but to answer your question just use

    
    
    info_string = lines[i + 1]
    value_str = info_string.split(" ",1)[-1].strip(" \"")
    

**BETTER SOLUTION**

do you have access to the php generating that .... if you do just do `echo
json_encode($data);` instead of using `var_dump`

if instead you have them output json it(the json output) will look like

    
    
    {"variable":"value","variable2","value2"}
    

you can then read it in like

    
    
    import json
    json_str = requests.get("http://url.com/json_dump").text  # or however you get the original text
    data = json.loads(json_str)
    print data
    

