【发布时间】:2017-10-17 17:08:28
【问题描述】:
下面的查询是抓取数据并创建一个 CSV 文件,我遇到的问题是名为“SPLE”的源在数据库中存储了数字为 0、1、50 的数据。
但是在 CSV 中,这些数字正在被收集到 CSV 中,我希望在创建 CSV 时以某种方式将这些数字表示为诸如,
0 = 真
1 = 错误
50 = 待处理
有人可以告诉我这是怎么做到的吗,我一直在努力解决这个问题? 我的代码:
从日期时间导入日期时间 从弹性搜索导入弹性搜索 导入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)
w.writerow(my_dict)
CSV 文件中的输出为:
Date RD Venue
20171016 1 Central
20171016 1 Central
20171016 0 Central
20171016 0 Central
20171016 50 Central
20171016 0 Central
20171016 1 Central
【问题讨论】:
标签: python python-3.x csv python-3.6