没有数据

时间:2017-10-18 12:43:50

标签: python python-3.x csv dictionary elasticsearch

当我运行以下查询并且["VT","NCR","N","DT","RD"],等值中没有数据时,查询失败。

错误消息

ValueError: dict contains fields not in fieldnames: ‘VT’

有没有办法说,如果任何值中没有数据仍然继续运行查询以获取python中具有数据的值的数据?
例如:'TRY','CATCH'或'PASS'方法?

我这几天一直在努力,有人能告诉我怎么做吗?

我的代码:

from datetime import datetime
from elasticsearch import Elasticsearch
import csv

es = Elasticsearch(["9200"])


res = es.search(index="search", body=
                {
                    "_source": ["VT","NCR","N","DT","RD"],
                    "query": {

                        "bool": {
                            "must": [{"range": {"VT": {
                                            "gte": "now/d",
                                            "lte": "now+1d/d"}}},

                                {"wildcard": {"user": "mike*"}}]}}},size=10)


csv_file = 'File_' + str(datetime.now().strftime('%Y_%m_%d - %H.%M.%S')) + '.csv'


header_names = { 'VT': 'Date', 'NCR': 'ExTime', 'N': 'Name', 'DT': 'Party', ' RD ': 'Period'}



with open(csv_file, 'w', newline='') as f:
    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,) 
            header_present = True
             w.writerow(my_dict)

1 个答案:

答案 0 :(得分:0)

我想在你的评论中指出一个缺陷

errmsg: 'aggregation result exceeds maximum document size (16MB)

实际上,它会在键的标题下写出字典的值...因此,你基本上会写两个标题。

关于错误according to the documentation,您可以忽略遗漏的字段,并在它们不存在时设置默认值

  

可选的 # will write DATE, TIME, ... in correct place w.writerow(header_names,) 参数指定在字典缺少restval中的密钥时要写入的值。如果传递给writerow()方法的字典包含在字段名中找不到的键,则可选的fieldnames参数指示要采取的操作。如果将其设置为' raise',则为默认值,引发ValueError。如果设置为'忽略',则忽略字典中的额外值。

例如

extrasaction

否则,请勿使用DictWriter并自行构成CSV行。您可以使用with open(csv_file, 'w', newline='') as f: # Open one csv for all the results w = csv.DictWriter(f, fieldnames=header_names.keys(), restval='', extrasaction='ignore') # There's only one header, don't need a boolean flag w.writeheader() # proceed to write results for doc in res['hits']['hits']: my_dict = doc['_source'] # Parse this dictionary however you need to write a valid CSV row w.writerow(my_dict) 提取值,但设置数据中不存在的默认值