Skip to content

Instantly share code, notes, and snippets.

@solidpple
Created August 3, 2016 00:18
Show Gist options
  • Select an option

  • Save solidpple/fd7e1f4bcf014b4559da8d0616681a5d to your computer and use it in GitHub Desktop.

Select an option

Save solidpple/fd7e1f4bcf014b4559da8d0616681a5d to your computer and use it in GitHub Desktop.
import json, os
import Proto import Tweet_pb2 as LogDataProto
import protobuf_to_dict import protobuf_to_dict
filename="tweet.log" # proto type
# read protobuf file
f = open(filename, "r")
try:
logfile = LogDataProto.LogDataFile()
logfile.ParseFromString(f.read())
except (IndexError, TypeError):
print 'index, type error'
# protobuf to json
logfile_dict = protobuf_to_dict(logfile)
json_string = json.dumps(logfile_dict, encoding='utf-8')
# write json file
new_filename = os.path.splitext(filename)[0] + '.json'
f = open(new_filename, 'w')
f.write(json_string.encode("utf-8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment