Skip to content

Instantly share code, notes, and snippets.

@linroex
Created September 14, 2019 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linroex/e3dc897d013d7b39ea9afdbbce44a2fe to your computer and use it in GitHub Desktop.
Save linroex/e3dc897d013d7b39ea9afdbbce44a2fe to your computer and use it in GitHub Desktop.
import csv
import json
from sys import argv
def parse_obj(obj):
for key in obj:
if isinstance(obj[key], str):
obj[key] = obj[key].encode('latin_1').decode('utf-8')
elif isinstance(obj[key], list):
obj[key] = list(map(lambda x: x if type(x) != str else x.encode('latin_1').decode('utf-8'), obj[key]))
pass
return obj
def main(input_path, output_path):
with open(input_path, 'r') as input_f, open(output_path, 'w', encoding='utf8') as output_f:
csv_output = csv.DictWriter(output_f, fieldnames=['timestamp', 'reaction', 'msg'])
records = json.load(input_f, object_hook=parse_obj)
csv_output.writeheader()
for i, record in enumerate(records['reactions']):
print( round((i+1) / len(records['reactions']) * 100, 4) )
record_obj = {
'timestamp': record['timestamp'],
'reaction': record['data'][0]['reaction']['reaction'],
'msg': record['title']
}
csv_output.writerow(record_obj)
if __name__ == '__main__':
# main("/Users/linroex/Downloads/posts_and_comments.json", "/Users/linroex/Desktop/fb_likes.csv")
main(argv[1], argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment