facebook - Storing Python dictionary data into a csv -
i have list of dicts stores facebook status data (graph api):
len(test_statuses) 3 test_statuses [{u'comments': {u'data': [{u'created_time': u'2016-01-27t10:47:30+0000', u'from': {u'id': u'1755814687982070', u'name': u'fadi cool panther'}, u'id': u'447173898813933_447182555479734', u'message': u'sidra abrar'}], u'paging': {u'cursors': {u'after': u'wti5dgjxvnvkrjlqzfhkemizstzorfeztvrneu5uvtforgm1tnpnme9qrtbove00t1rfmk5uqt0=', u'before': u'wti5dgjxvnvkrjlqzfhkemizstzorfeztvrneu5uvtforgm1tnpnme9qrtbove00t1rfmk5uqt0='}}, u'summary': {u'can_comment': false, u'order': u'ranked', u'total_count': 1}}, u'created_time': u'2016-01-27t10:16:56+0000', u'id': u'5842136044_10153381090881045', u'likes': {u'data': [{u'id': u'729038357232696'}, {u'id': u'547422955417520'}, {u'id': u'422351987958296'}, {u'id': u'536057309903473'}, {u'id': u'206846772999449'}, {u'id': u'1671329739783719'}, {u'id': u'991398107599340'}, {u'id': u'208751836138231'}, {u'id': u'491047841097510'}, {u'id': u'664580270350825'}], u'paging': {u'cursors': {u'after': u'njy0ntgwmjcwmzuwodi1', u'before': u'nzi5mdm4mzu3mjmynjk2'}, u'next': u'https://graph.facebook.com/v2.5/5842136044_10153381090881045/likes?limit=10&summary=true&access_token=521971961312518|121ca7ef750debf4c51d1388cf25ead4&after=njy0ntgwmjcwmzuwodi1'}, u'summary': {u'can_like': false, u'has_liked': false, u'total_count': 13}}, u'link': u'https://www.facebook.com/ukbhangrasongs/videos/447173898813933/', u'message': u'track : ik waar ( official music video )\nsinger : falak shabir ft dj shadow\nmusic dj shadow\nfor more : uk bhangra songs', u'shares': {u'count': 7}, u'type': u'video'}, {u'comments': {u'data': [], u'summary': {u'can_comment': false, u'order': u'chronological', u'total_count': 0}}, u'created_time': u'2016-01-27t06:15:40+0000', u'id': u'5842136044_10153380831261045', u'likes': {u'data': [], u'summary': {u'can_like': false, u'has_liked': false, u'total_count': 0}}, u'message': u'i want work you. tracks flicks', u'type': u'status'}]
i need extract each status text , text of each comment under status, can appending them separate lists e.g.,:
status_text = [] comment_text = [] s in test_statuses: try: status_text.append(s['message']) c in s['comments']['data']: comment_text.append(c['message']) except: continue
this gives me 2 lists of separate lengths len(status_text)
= 2
, len(comment_text)
= 49
.
unfortunately that's horrible way of dealing data since cannot keep track of comment belongs status. ideally store tree structure , export in cvs file, can't figure out how it.
probable data acv data structure:
text is_comment status1 0 status2 0 statusn 0 comment1 status1 comment2 status1 commentn statusn
why need in csv? structured , ready persisted json.
if need tabular approach offered csv, have either denormalize it, or use more 1 csv table references 1 (and again, best approach put data in sql database takes care of relationships you)
that said, way denormalize save same status text each row comment - is: record csv row in innermost loop approach:
import csv status_text = [] comment_text = [] writer = csv.writer(open("mycsv.csv", "wt")) s in test_statuses: test_messages.append(s['message']) c in s['comments']['data']: test_comments.append(c['message']) writer.writerow((s['message'], c['message']))
note you'd better writing status id
to each row, , create second table status message id
key (and put in database instead of various csv files). , then, again, better of keeping json. if need search capabilities, use json capable database such mongodb or postgresql
Comments
Post a Comment