簡體   English   中英

彈性搜索導出的CSV文件中可讀的列名稱?

[英]readable columns names in my CSV file that is exported from elastic search?

以下是從彈性搜索中獲取一些數據並將其導出到名為“ mycsvfile”的csv文件的代碼。 我想更改列名,以便人類可以讀取。 下面是代碼:

from elasticsearch import Elasticsearch
import csv

es = Elasticsearch(["9200"])

# Replace the following Query with your own Elastic Search Query
res = es.search(index="search", body=
                {
                    "_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
                    "query": {
                        "bool": {
                            "should": [
                                {"wildcard": {"CN": "TEST1"}}

                            ]
                        }
                    }
}, size=10)



with open('mycsvfile.csv', 'w') as f:  # Just use 'w' mode in 3.x
    header_present  = False
    for doc in res['hits']['hits']:
        my_dict = doc['_source'] 
        if not header_present:
            w = csv.DictWriter(f, my_dict.keys())
            w.writeheader()
            header_present = True


        w.writerow(my_dict)

當我運行上述查詢時,CSV文件數據如下所示:

DTDT    TRDT    SPLE    SACL    RPLE

20170512    12/05/2017 15:39    1001    0   0

20170512    12/05/2017 15:39    1001    0   0

20170908    08/09/2017 02:42    1001    0   0

20170908    08/09/2017 06:30    1001    0   0

如您所見,列名與查詢中的列名相同,並且我希望在生成文件時為其賦予可讀名稱。 例如,我想使用DATE而不是DTDT,而TRDT將是TIME等

有人可以替我顯示並修改我的代碼,以便將列名稱輸入CSV文件嗎?

先感謝您

編輯:對不起,那條線是我寫的。 正確的,經過測試的版本如下。

with open('mycsvfile.csv', 'w') as f:  # Just use 'w' mode in 3.x
    header_present  = False
    for doc in res['hits']['hits']:
        my_dict = doc['_source'] 
        if not header_present:
            fieldnames = ['name', 'name', 'name']
            w = csv.DictWriter(f, fieldnames=fieldnames)
            w.writeheader()
            header_present = True

        w.writerow(my_dict)

使腳本寫出標頭的原因是傳遞給DictWriter的my_dict.keys()。 用標簽列表替換這些鍵,編寫者應該正確地編寫它。

一種簡單的方法是僅將字典用作轉換表並將其寫為行,而不是編寫實際的DictWriter標頭:

header_names = { 'DTDT': 'DATE', 'TRDT': 'TIME', ...}

with open('mycsvfile.csv', 'w') as f:  # Just use 'w' mode in 3.x
    header_present  = False
    for doc in res['hits']['hits']:
        my_dict = doc['_source'] 
        if not header_present:
            w = csv.DictWriter(f, my_dict.keys())
            w.writerow(header_names)  # will write DATE, TIME, ... in correct place
            header_present = True


        w.writerow(my_dict)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM