How can I extract specific elements out of a unstructured list and put them into a dataframe using Python
Question: I have a long list of strings and I want to extract only rows that have
"Town":"Some City" & "State":"Some State" and then put those values into a
dataframe with town and state as column headers. I've copied an extract of the
strings below (it excludes the beginning [ and ending ] because the list is
really long. Any ideas?

    
    
    '      "IsPayAtLocation": null,',
     '      "IsMembershipRequired": null,',
     '      "IsAccessKeyRequired": null,',
     '      "ID": 1,',
     '      "Title": "Public"',
     '    },',
     '    "UsageCost": "Free",',
     '    "AddressInfo": {',
     '      "ID": 57105,',
     '      "Title": "Somerset North",',
     '      "AddressLine1": "2800 W. Big Beaver Rd",',
     '      "AddressLine2": null,',
     '      "Town": "Troy",',
     '      "StateOrProvince": "MI",',
     '      "Postcode": "48084",',
     '      "CountryID": 2,',
     '      "Country": {',
     '        "ISOCode": "US",'
    


Answer: 
    ^[^,]*\b(?:Town|State).*$
    

You can use this `re.findall`.See demo.

<https://regex101.com/r/hE4jH0/34>

    
    
    import re
    p = re.compile(r'^[^,]*\b(?:Town|State).*$', re.MULTILINE)
    test_str = "\"UsageCost\"', ' \"Free\",']\n['    \"AddressInfo\"', ' {']\n['      \"ID\"', ' 57105,']\n['      \"Title\"', ' \"Somerset North\",']\n['      \"AddressLine1\"', ' \"2800 W. Big Beaver Rd\",']\n['      \"AddressLine2\"', ' null,']\n['      \"Town\"', ' \"Troy\",']\n['      \"StateOrProvince\"', ' \"MI\",']\n['      \"Postcode\"', ' \"48084\",']\n['      \"CountryID\"', ' 2,']\n['      \"Country\"', ' {']\n['        \"ISOCode\"', ' \"US\",']\n['        \"ContinentCode\"', ' \"NA\",']\n['        \"ID\"', ' 2,']\n['        \"Title\"', ' \"United States\"']"
    
    re.findall(p, test_str)
    

