Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16714

Receiving KeyError for a key that exists [closed]

$
0
0

So I wanted to try out a Machine Learning project to learn more about it, and decided to build a model that predicts bitcoin prices. When I try to retrieve the comment from each wiki revision, I get a key error saying 'comment' key doesn't exist.

edits = {}for rev in revs:    date = time.strftine("%Y-%m-%d", rev["timestamp"])    if date not in edits:        edits[date]=dict(sentiments=list(), edit_count=0)    edits[date]["edit_count"] += 1    comment = rev["comment"]    edits[date]["sentiments"].append(find_sentiment(comment))

Output

    -------------------------------------------------------------------    KeyError                            Traceback (most recent call last)    Cell In[10], line 10         8   edits[date]=dict(sentiments=list(), edit_count=0)         9 edits[date]["edit_count"] += 1    --->10 comment = rev['comment']        11 edits[date]["sentiments"].append(find_sentiment(comment))    KeyError: 'comment'

When I looked at the dictionary, it had the comment key. I tried looking up the problem but everybody just says to handle the exception by printing an error message, and I need the data. Any help would be greatly appreciated, I've had so many problems on this already and this seems like a dead end.

revs = sorted(revs, key=lambda rev: rev["timestamp"])revs[0]

Output

 OrderedDict([('revid', 275832581),             ('parentid', 0),             ('user', 'Pratyeka'),             ('timestamp',              time.struct_time(tm_year=2009, tm_mon=3, tm_mday=8, tm_hour=16, tm_min=41, tm_sec=7, tm_wday=6, tm_yday=67, tm_isdst=-1)),             ('comment', 'creation (stub)')])

Also here is the project I was trying to follow if this would help:Video


Viewing all articles
Browse latest Browse all 16714

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>