下面的查询正在获取数据并创建一个CSV文件,我遇到的问题是名为“ SPLE”的源将数据存储在数据库中,其编号为0、1、50。

但是,在CSV中,这些数字正在CSV中收集,我想在创建CSV时以某种方式表示这些数字,例如,

0 =真

1 =错误

50 =待定

有人可以告诉我如何做到这一点,我一直在努力吗?
我的代码:

从datetime导入datetime
    从elasticsearch导入Elasticsearch
    导入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

最佳答案

看起来您有点复杂,Pandas是您的朋友。

import pandas as pd

def SPLE_fix(sple):
    if sple == 0:
        return('True')
    elif sple == 1:
        return('False')
    else:
        return('Pending')


df=pd.read_csv('mycsvfile.csv')

df['SPLE'] = df['SPLE'].apply(SPLE_fix)

df.to_csv('newcsv.csv', index=False)


newcsv.csv的输出:

Date,SPLE,Venue
20171016,False,Central
20171016,False,Central
20171016,True,Central
20171016,True,Central
20171016,Pending,Central
20171016,True,Central
20171016,False,Central


编辑:

对于无熊猫的解决方案:

import csv

def SPLE_fix(sple):
    #just check for text in header
    try:
        sple[1]=int(sple[1])
    except:
        return(sple)

    #this part just changes the value
    if sple[1] == 0:
        sple[1] = 'True'
    elif sple[1] == 1:
        sple[1] = 'False'
    else:
        sple[1] = 'Pending'

    return(sple)


with open('mycsvfile.csv', 'r') as csvfile:
    data=csv.reader(csvfile, delimiter=',')
    new_data=[SPLE_fix(row) for row in data]

with open('newcsv.csv', 'w', newline='') as csvfile:
    cwrite=csv.writer(csvfile, delimiter=',')
    cwrite.writerows(new_data)


这应该得到相同的结果。可能不是最有效的方法,但是除非您进行大量操作,否则不要紧。

10-02 03:42
查看更多