下面的查詢是抓取數據並創建CSV文件,我遇到的問題是名爲'SPLE'的源將數據存儲在數據庫中,數字爲0,1,50 。將數值數據更改爲CSV文件中的文本
然而在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 1 Central
20171016 0 Central
20171016 0 Central
20171016 50 Central
20171016 0 Central
20171016 1 Central
FYI:
我曾嘗試使用熊貓但是我無法安裝熊貓,所以我我想知道是否有其他解決方法?
在for循環中的w.writerow(my_dict)之前,根據需要更改'my_dict'的內容。 – ZdaR
你能告訴我嗎?我想我做錯了 – Rich