elasticsearch,python-3.6,Python,Python 3.x,Csv,elasticsearch,Python 3.6" /> elasticsearch,python-3.6,Python,Python 3.x,Csv,elasticsearch,Python 3.6" />

Python 从弹性搜索导出的我的CSV文件中的可读列名称?

Python 从弹性搜索导出的我的CSV文件中的可读列名称?,python,python-3.x,csv,elasticsearch,python-3.6,Python,Python 3.x,Csv,elasticsearch,Python 3.6,下面是从弹性搜索中获取一些数据并将这些数据导出到名为“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

下面是从弹性搜索中获取一些数据并将这些数据导出到名为“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
正如您所看到的,列名与查询中的列名相同,我希望在生成文件时为它们提供可读的名称。例如,我想用日期代替DTDT,而TRDT是时间等等

是否有人可以显示并修复我的代码,以便我在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()。将这些键替换为标签列表,编写者应正确编写

一种简单的方法是将dict用作转换表,并将其作为行写入,而不是写入实际的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)

我用你的答案-w.writeheader('column\u name','column\u name','column\u name','column\u name','column\u name')替换了“w.writerow(我的字典)”。但是我现在得到一个错误,它是-Traceback(最近一次调用last):文件“C:/Users/.PyCharmCE2017.2/config/scratches/test1.py”,第28行,在w.writeheader('column_name','column_name','column_name','column_name','column_name')TypeError:writeheader()接受1个位置参数,但给出了5个。是因为我在w.writeheader中获得了我的dict吗?感谢您的关注,我运行了代码并创建了文件,但它不再抓取我的数据了?代码中有什么遗漏吗?这很有效!非常感谢你!您知道如何创建名为“mycsvfile”的文件,在文件名的末尾显示当前日期和时间,以便在运行查询时使用,文件名应为mycsvfile20121012-08:46