Using re.findall in python outputting one set of parameters rather than a set of parameters for each line
Question: I've used `readlines` to split all of the sentences in a file up and I want to
use `re.findall` to go through and find the capitals within them. However, the
only output I can get is one set of capitals for all the sentences but I want
a set of capitals for each sentence in the file.

I'm using a for loop to attempt this at the moment, but I'm not sure whether
this is the best course of action with this task.

Input:

    
    
    Line 01: HE went to the SHOP
    Line 02: THE SHOP HE went
    

This is what I'm getting as an output:

    
    
    [HE, SHOP, THE]
    

and I want to get the output:

    
    
    [HE, SHOP], [THE, SHOP, HE]
    

Is there a way of doing this? I've put my coding at the minute below. Thanks!

    
    
    import re, sys
    
    f = open('findallEX.txt', 'r')
    
    lines = f.readlines()
    
    ii=0
    
    for l in lines:
        sys.stdout.write('line %s: %s' %(ii, l))
        ii = ii + 1
    
    for x in l
        re.findall('[A-Z]+', l)
    print x
    


Answer: I think the way to do that is as follows:

    
    
    txt = """HE went to the SHOP
    THE SHOP HE went"""
    
    result = []
    for s in txt.split('\n'):
        result += [re.findall(r'[A-Z]+', s)]
    
    print(result) # prints [['HE', 'SHOP'], ['THE', 'SHOP', 'HE']] 
    

Or using list comprehensions (a bit less readable):

    
    
    txt = """HE went to the SHOP
        THE SHOP HE went"""
    
    print([re.findall(r'[A-Z]+', s) for s in txt.split('\n')])
    

