以下查询是抓取数据并创建CSV文件,我遇到的问题是名为“SPLE”的源将数据存储在数据库中,数字为0,1,50。
然而,在CSV中,这些数字正在CSV中收集,我想在某种程度上创建CSV时,这些数字代表单词,例如,
0 =真
1 =假
50 =等待
有人可以告诉我这是怎么做的,我一直在努力吗?
我的代码:
from elasticsearch import Elasticsearch
import csv
es = Elasticsearch(["9200"])
res = es.search(index="search", body=
{
"_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
"query": {
"bool": {
"should": [
{"wildcard": {"CN": "TEST*"}}
]
}
}
}, size=10)
header_names = { 'DTDT': 'DATE', 'SPLE': 'TAG', ...}
with open('mycsvfile.csv', 'w') 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)
CSV文件中的输出为:
Date SPLE Venue
20171016 1 Central
20171016 0 Central
20171016 50 Central
答案 0 :(得分:0)
我假设mycsvfile.csv
文件有SPLE列。
from elasticsearch import Elasticsearch
es = Elasticsearch(["9200"])
res = es.search(index="search", body=
{
"_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
"query": {
"bool": {
"should": [
{"wildcard": {"CN": "TEST*"}}
]
}
}
}, size=10)
import pandas as pd
SPLE = {0:'true',1:'false',50:'pending'}
saved_csv = pd.read_csv('mycsvfile.csv',sep='\t')
saved_csv['SPLE'] = saved_csv['SPLE'].map(lambda x: SPLE[int(x)])
saved_csv.to_csv('edited_csv.csv', index=False)
答案 1 :(得分:0)
在某个地方宣布一个字典进行翻译:
SPLE_TRANSLATION = {0: 'True', 1: 'False', 50: 'Pending'}
然后,在你的循环中:
my_dict['SPLE'] = SPLE_TRANSLATION[my_dict['SPLE']]
w.writerow(my_dict)