반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

[python] 숫자 3자리 마다 콤마(,)찍기 - format 본문

카테고리 없음

[python] 숫자 3자리 마다 콤마(,)찍기 - format

닉의네임 2024. 2. 6. 16:25
반응형

결론부터

format(buckets["doc_count"],',')

 

그렇다 이렇게 하면 숫자 3자리 마다  콤마(,) 가 찍힌다. 

날짜구간: 2024-02-05 ~ 2024-02-06
확장검색 전체: 114,274건
 * 일반 쿼리: 113,675건
 * 슬로우 쿼리: 599건

 - 확장검색 상세 - 
 * 0ms ~ 1000ms: 111,250건
 * 1000ms ~ 2000ms: 1,708건
 * 2000ms ~ 3000ms: 717건
 * 3000ms ~ : 599건

 - 확장검색 슬로우 상세 - 
 * 3000ms ~ 4000ms: 325건
 * 4000ms ~ 5000ms: 147건
 * 5000ms ~ : 127건

 - 검색결과 상세 - 
 * 1 ~ 100: 58,322건
 * 100 ~ 1000: 12,161건
 * 1000 ~ 10000: 25,137건
 * 10000 ~ : 18,654건

 

전체코드 

# -*- coding: utf-8 -*-

import time
import json

from datetime import datetime, timedelta

import requests
import ssl
import urllib3
from time import sleep
from elasticsearch import Elasticsearch

print(ssl.OPENSSL_VERSION)
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


def get_keyword():
    f_v = open("./data/search_report.txt",'w')
    now =datetime.now() - timedelta(days=1)
    start = now.date().strftime('%Y-%m-%d %H:%M:%S')
    start_v = now.date().strftime('%Y-%m-%d')
    end_date = now + timedelta(days=1)
    end = end_date.date().strftime('%Y-%m-%d %H:%M:%S')
    end_v = end_date.date().strftime('%Y-%m-%d')

    with open(LOG) as index_file:
        source = index_file.read().strip()
        query = source.replace("{start}", start).replace("{end}", end)
        response = log_client.search(index=LOG_INDEX, body=query)
        keys = []
        print("== log == ")

        print("날짜구간: "+ start_v + " ~ " + end_v)
        f_v.write("날짜구간: "+ start_v + " ~ " + end_v)
        f_v.write("\n")
        f_v.write("확장검색 전체: "  + str(format(response["hits"]["total"]["value"],',')) + "건\n")
    for buckets in response["aggregations"]["COUNT"]["buckets"]:
        f_v.write(" * "+buckets["key"] + ": ")
        f_v.write(str(format(buckets["doc_count"],',')) + "건\n")

    f_v.write("\n")
    f_v.write(" - 확장검색 상세 - \n")
    for buckets in response["aggregations"]["NORMAL"]["buckets"]:
        f_v.write(" * "+buckets["key"] + ": ")
        f_v.write(str(format(buckets["doc_count"],',')) + "건\n")

    f_v.write("\n")
    f_v.write(" - 확장검색 슬로우 상세 - \n")
    for buckets in response["aggregations"]["SLOW"]["buckets"]:
        f_v.write(" * "+buckets["key"] + ": ")
        f_v.write(str(format(buckets["doc_count"],',')) + "건\n")

    f_v.write("\n")
    f_v.write(" - 검색결과 상세 - \n")
    for buckets in response["aggregations"]["RECALL"]["buckets"]:
        f_v.write(" * "+buckets["key"] + ": ")
        f_v.write(str(format(buckets["doc_count"],',')) + "건\n")

    f_v.close()

##### MAIN SCRIPT #####

if __name__ == '__main__':

    LOG_INDEX = "home-search-query-log"
    LOG = "./sql/search-log.json"
    log_client = Elasticsearch("https://id:pw@host:port/", ca_certs=False,
                               verify_certs=False)
    print("Start.")
    get_keyword()
    print("END.")

 

반응형
Comments