我無法遍歷由pickle讀取的列表循環。此代碼的最終目標是循環遍歷每個項目並返回每個項目的ID號碼。遍歷由pickle讀取的列表以查找用戶標識
## Opening the file, and loading it into a list##
with open('TEMP_ITEMS.txt', 'rb') as openfile:
items = pickle.load(openfile)
我在通過這個嘗試循環,並找到ID號的嘗試基於一些舊的XML抓取技術,但由於某些原因的邏輯在這裏並不適用。
for item in enumerate(items):
pattern0 = re.compile('ID: (.*?) <br>')
idnumber = float(re.findall(pattern0, items[0])[0])
print "ID Number: ",idnumber
的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.
在這個問題上的任何幫助或建議的內容實施例,將不勝感激。親切的問候AEA下falsetru建議使用
代碼,它返回一個錯誤
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
錯誤的代碼它生產:
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
>>>
'pattern0 = re.compile(...); re.findall(pattern0)'沒有'item'的商業? – iMom0
Hello @ iMom0我仍然在學習編碼,如果某件事看起來非常明顯錯誤,那麼它可能就是。 – AEA