我正在尝试在CSV中的两列上使用“NCR”中的数据。但是它会相互覆盖,只会在名为“YesterdayTime”的列中显示数据。
有没有办法在名为’ExTime’
和‘YesterdayTime’
的列中使用“NCR”中的数据?
我的代码
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*"}},
{"wildcard": {"user": "jane*"}},
{"wildcard": {"user": "kate*"}},
{"wildcard": {"user": "dave*"}},
{"wildcard": {"user": "rich*"}}
]}}},size=10)
csv_file = 'File_' + str(datetime.now().strftime('%Y_%m_%d - %H.%M.%S')) + '.csv'
header_names = { 'VT': 'Date', 'NCR': 'ExTime', 'NCR': 'YesterdayTime', 'N': 'Name', 'DT': 'Party', ' RD ': 'Period'}
with open(csv_file, 'w', newline='') as f:
w = csv.DictWriter(f, fieldnames=header_names.keys(), restval='', extrasaction='ignore')
w.writerow(header_names,)
for doc in res['hits']['hits']:
my_dict = doc['_source']
w.writerow(my_dict)
输出 - EX time
正在替换为YesterdayTime
。
DATE YesterdayTime Name Party Period
20170512 12/05/2017 15:39 1001 0 0
20170512 12/05/2017 15:39 1001 0 0
20170908 08/09/2017 02:42 1001 0 0
20170908 08/09/2017 06:30 1001 0 0
我想要的正确输出:
DATE YesterdayTime YesterdayTime Name Party Period
20170512 12/05/2017 15:39 12/05/2017 15:39 1001 0 0
20170512 12/05/2017 15:39 12/05/2017 15:39 1001 0 0
20170908 08/09/2017 02:42 08/09/2017 02:42 1001 0 0
20170908 08/09/2017 06:30 08/09/2017 06:30 1001 0 0
答案 0 :(得分:0)
Python词典不支持重复键。您在header_names字典中最后一次出现'NSC'会覆盖第一个。
答案 1 :(得分:0)
您可以为字段名指定包含重复项的列表。
with open('test.csv', 'w') as f:
csv_file = csv.DictWriter(f, fieldnames=['foo', 'bar', 'bar', 'bar'])
csv_file.writeheader()
csv_file.writerow({'foo':1, 'bar':2})
test.csv包含:
foo,bar,bar,bar
1,2,2,2
但是,如果您需要不同的标题名称,则无法解决问题...