Looping through a list read by pickle to find userid
Question: I am having trouble looping through a list read by pickle. The Ultimate aim of
this code was to loop through each Item and return the id number of each item.

    
    
    ## Opening the file, and loading it into a list##
    with open('TEMP_ITEMS.txt', 'rb') as openfile:
        items = pickle.load(openfile)
    

My attempt at trying to loop through this and find the id numbers was based on
some old xml scraping techniques, but for some reason the logic doesn't apply
here.

    
    
    for item in enumerate(items):
    
        pattern0 = re.compile('ID: (.*?) <br>')
        idnumber = float(re.findall(pattern0, items[0])[0])
        print "ID Number: ",idnumber 
    

Example of the contents of TEMP_ITEMS.txt

    
    
    (lp0
    S'\n                <item>\n                    <title>Timmy</title>\n                    <link>caturl</link>\n                    <description><![CDATA[\n                                Timmy <br>\n                                ID: 3712 <br>\n                                Age: 10 <br>\n                                Weight: 7lbs <br>\n                                Time: 17:23 <br>\n                                Cat Name: Timmy <br>\n\n                    ]]></description>\n                    <guid isPermaLink="false">04e72b29-065d-4893-a4d2-f16ff30a283e</guid>\n                    <pubDate>Fri, 21 Jun 2013 01:09:05 GMT</pubDate>\n                </item>'
    p1
    aS'\n                <item>\n                    <title>George</title>\n                    <link>caturl</link>\n                    <description><![CDATA[\n                                George <br>\n                                ID: 4124 <br>\n                                Age: 14 <br>\n                                Weight: 8lbs <br>\n                                Time: 15:41 <br>\n                                Cat Name: George <br>\n\n                    ]]></description>\n                    <guid isPermaLink="false">212f9fbf-564b-470a-a64a-ef51036ff06a</guid>\n                    <pubDate>Fri, 21 Jun 2013 01:28:20 GMT</pubDate>\n                </item>'
    p2
    a.
    

Any help or advice on this problem would be greatly appreciated. Kind regards
AEA

# Code used under recommendations of falsetru, which returns an error

    
    
    import pickle
    import re
    
    with open('TEMP_RSS_ITEMS.txt', 'rb') as temp_rss_items_open4:
        items = pickle.load(temp_rss_items_open4)        
        print items
        for item in enumerate(items):
            pattern0 = re.compile('ID: (.*) <br>')
            for idnumber in re.findall(pattern0, item):
                print idnumber
    

Error this code it producing:

    
    
    Traceback (most recent call last):
      File "C:/Sharing/test1.py", line 9, in <module>
        for idnumber in re.findall(pattern0, item):
      File "C:\Python27\lib\re.py", line 177, in findall
        return _compile(pattern, flags).findall(string)
    TypeError: expected string or buffer
    >>> 
    


Answer: Try using a non-greedy version of `.*`:

    
    
    pattern0 = re.complie(r'ID: (.*?) <br>')
    

or '+` if ID has only digits:

    
    
    pattern0 = re.complie(r'ID: (\d+)')
    

**UPDATE**

    
    
    import pickle
    import re
    
    pattern0 = re.compile('ID: (.*) <br>')
    with open('TEMP_RSS_ITEMS.txt', 'rb') as f:
        items = pickle.load(f)        
        for item in items:
            for idnumber in pattern0.findall(item):
                print idnumber
    

