사전 개체를 cPickle을 사용하여 파일에 저장하고 있는데, 첫 번째 항목이 아닌 다른 항목을 가져올 수 없습니다. 초기에 파일 tweets.pkl
은 비어 있고 EOFError
가 발생합니다. 나는 그것이 그것과 관련이 있다고 확신한다. 새 사전은 이전의 모든 참조를 잃게 있도록 감사cPickle을 사용하면 파일의 첫 번째 항목 만 반환됩니다.
#!/usr/bin/env python
from urllib import urlencode, urlopen
from simplejson import loads
from hashlib import md5
from collections import defaultdict
import json
import cPickle as pickle
def fetch_tweets(new_feeds):
dic = json.loads(new_feeds)
feeds_file = open('tweets.pkl','r+b')
try:
feeds = pickle.load(feeds_file)
except EOFError:
#THIS IS BAD
feeds = defaultdict()
feeds_file.close()
# RETURNS ONLY THE FIRST FEED ENTRY
for i in feeds.iteritems():
print str(i)
for i in dic['results']:
hash = computeHash(i['text'])
if hash not in feeds:
appendfeed(hash, i, 'tweets.pkl')
def appendfeed(hash, new_feed, file):
feed = defaultdict()
file = open(file, 'a+b')
feed[hash] = new_feed
pickle.dump(feed, file)
file.close()
def computeHash(data):
h = md5(data.encode('utf-8'))
return h.hexdigest()
을 참조하십시오. 나는 선반에 대해 몰랐다. 더 효율적인 방법입니다. – ajmartin
도와 드리겠습니다. 마운틴 뷰의 바로 남쪽에서 감사합니다 :) – phooji