Skip to content

Instantly share code, notes, and snippets.

@yuanotes
Created August 21, 2016 08:32
Show Gist options
  • Save yuanotes/62fdd3b03a67e7d6bb9b1c3c61816805 to your computer and use it in GitHub Desktop.
Save yuanotes/62fdd3b03a67e7d6bb9b1c3c61816805 to your computer and use it in GitHub Desktop.
Process elastic search json result to csv.
# coding=utf-8
import re
import os.path
import sys
import json
PATH = os.path.dirname(os.path.abspath(__file__))
def get_file_name(name):
result = re.search(r'(\w+)\.[^\.]+$', name)
if result:
return result.group(1)
else:
return name
# RESP_FILE = os.path.join(PATH, sys.argv[1])
RESP_FILE = os.path.join(PATH, "ydy_0821.json")
CSV_FILE = "%s.csv" % get_file_name(RESP_FILE)
with open(RESP_FILE, 'r') as in_file:
with open(CSV_FILE, 'w') as out_file:
in_json = json.load(in_file)
result_map = in_json["aggregations"]["2"]["buckets"]
out_file.write('运动员,声量\n')
for ydy_name in result_map.keys():
buckets = result_map[ydy_name]["3"]["buckets"]
for bucket in buckets:
ydy_data = [ydy_name]
ydy_data.append(bucket["key_as_string"].replace("T00:00:00.000+08:00",""))
ydy_data.append(str(bucket["doc_count"]))
out_file.write((",".join(ydy_data) + '\n').encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment