Python: how to get correct list when split?
Question: In test.txt, I have 2 lines of sentences.

    
    
    The heart was made to be broken.
    There is no surprise more magical than the surprise of being loved.
    

The code:

    
    
    import re
    file = open('/test.txt','r')#specify file to open
    data = file.readlines()
    file.close()
    for line in data:
        line_split = re.split(r'[ \t\n\r, ]+',line)
        print line_split
    

Results from the codes:

    
    
    ['The', 'heart', 'was', 'made', 'to', 'be', 'broken.', '']
    ['There', 'is', 'no', 'surprise', 'more', 'magical', 'than', 'the', 'surprise', 'of', 'being', 'loved.']
    

How to get only word print out? (see the first sentence) Expect result:

    
    
    ['The', 'heart', 'was', 'made', 'to', 'be', 'broken.']
    ['There', 'is', 'no', 'surprise', 'more', 'magical', 'than', 'the', 'surprise', 'of', 'being', 'loved.']
    

Any advice?


Answer: Instead of using `split` to match the delimiters, you can use `findall` with
the negated regular expression to match the parts you want to keep:

    
    
    line_split = re.findall(r'[^ \t\n\r., ]+',line)
    

See it working online: [ideone](http://ideone.com/OV2dq)

