Pulling out quotes from within a text file
Question: Let's say I have a text file in python that says:

    
    
    the data starts
    test Age="0" Order="51" Doctor-ID="XX2342"
    test Age="0" Order="53" Doctor-ID="XX2342"
    end of data
    

What code would return:

    
    
    "0" "51" "XX2342"
    "0" "53" "XX2342"
    

Returning lists would also work.

    
    
    [["0","51","XX2342"]
    ["0","53","XX2342"]]
    

Thank you!


Answer: This is a perfect job for regex

    
    
    line = 'test Age="0" Order="51" Doctor-ID="XX2342"'
    import re
    re.findall('"(.*?)"', line)
    >>> ['0', '51', 'XX2342']
    

For operating on multiple lines:

    
    
    lines = '''
    test Age="0" Order="51" Doctor-ID="XX2342"
    test Age="0" Order="53" Doctor-ID="XX2342"
    '''
    results = []
    for line in lines.split('\n'):
        result = re.findall('"(.*?)"', line)
        if result:
            results.append(result)
    
    for result in results:
        print result
    

This gives:

    
    
    ['0', '51', 'XX2342']
    ['0', '53', 'XX2342']
    

