【问题标题】:Export Elasticsearch data results into a CSV file将 Elasticsearch 数据结果导出到 CSV 文件中
【发布时间】:2018-03-22 21:28:13
【问题描述】:

我想将从我的查询返回的弹性搜索数据导出到 CSV 文件。我一直在到处寻找这样做,但我在这方面的所有尝试都失败了。

我的查询如下;

import elasticsearch
import unicodedata
import csv

es = elasticsearch.Elasticsearch(["9200"])
# this returns up to 100 rows, adjust to your needs

res = es.search(index="search", body=
{
    "_source": ["CN", "NCR", "TRDT"],

    "query": {
                        "bool": {
                            "should": [
                                {"wildcard": {
                                        "CN": "TEST*"
                                    }
                                }]
                        }
                    }},size=10)

sample = res['hits']['hits']

# then open a csv file, and loop through the results, writing to the csv
with open('outputfile.tsv', 'wb') as csvfile:

    filewriter = csv.writer(csvfile, delimiter='\t',  # we use TAB delimited, to handle cases where freeform text may have a comma
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
    # create column header row
    filewriter.writerow(["CN", "NCR", "TRDT"])    #change the column labels here
    # fill columns 1, 2, 3 with your data
    col1 = hit["some"]["deeply"]["nested"]["field"].decode('utf-8')  #replace these nested key names with your own
    col1 = col1.replace('\n', ' ')
    # col2 = , col3 = , etc...
    for hit in sample:
        filewriter.writerow(col1)

请有人修复此代码以使其工作,花费数小时使其工作。

我在运行时遇到的错误如下:

Traceback (most recent call last):
  File "C:/Users/.PyCharmCE2017.2/config/scratches/trtr.py", line 30, in <module>
    filewriter.writerow(["CN", "NCR", "TRDT"])  # change the column labels here
TypeError: a bytes-like object is required, not 'str'

提前谢谢你。

【问题讨论】:

    标签: python python-3.x csv elasticsearch export-to-csv


    【解决方案1】:

    with open('outputfile.tsv', 'wb') 中将wb 替换为w,因为您使用的是Python 3.x

    【讨论】:

    • 谢谢,解决了这个问题,但现在我得到了另一个问题,即: Traceback(最近一次通话最后一次):文件“C:/Users/.PyCharmCE2017.2/config/scratches/trtr. py", line 32, in col1 = sample["some"]["deeply"]["nested"]["field"].decode('utf-8') # 用你的替换这些嵌套的键名own TypeError: list indices must be integers or slices, not str
    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多