How to create a filename with the current date and time in python when query is ran

南楼画角 提交于 2020-01-03 02:25:50

问题


When I run my query below, it creates a file called ‘mycsvfile’. However is there a way to add the current date and timestamp when the CSV file is created? For example if I run this query now the file should be named mycsvfile20171012 – 10:00:00 (something like that).

Could someone edit my code and show me how to do this please?

My code:

from elasticsearch import Elasticsearch
import csv

es = Elasticsearch(["9200"])

# Replace the following Query with your own Elastic Search Query
res = es.search(index="search", body=
                {
                    "_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
                    "query": {
                        "bool": {
                            "should": [
                                {"wildcard": {"CN": "TEST1"}}

                            ]
                        }
                    }
}, size=10)



header_names = { 'DTDT': 'DATE', 'TRDT': 'TIME', ...}

with open('mycsvfile.csv', 'w') as f:  # Just use 'w' mode in 3.x
    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)  # will write DATE, TIME, ... in correct place
            header_present = True


        w.writerow(my_dict)

Thank you in advance!


回答1:


It is better to use underscore in filename than any other special character since it widely accepted Therefore constructing file name as below :

csv_file = 'myfile_' + str(datetime.now().strftime('%Y_%m_%d_%H_%M_%S')) + '.csv'

Use datetime as below :

from elasticsearch import Elasticsearch
import csv

es = Elasticsearch(["9200"])

# Replace the following Query with your own Elastic Search Query
res = es.search(index="search", body=
                {
                    "_source": ["DTDT", "TRDT", "SPLE", "RPLE"],
                    "query": {
                        "bool": {
                            "should": [
                                {"wildcard": {"CN": "TEST1"}}

                            ]
                        }
                    }
}, size=10)

from datetime import datetime
import os

file_path = <PASS YOUR FILE HERE>

csv_file = 'myfile_' + str(datetime.now().strftime('%Y_%m_%d_%H_%M_%S')) + '.csv'

csv_file_full = os.path.join(file_path, os.sep, csv_file)

header_names = { 'DTDT': 'DATE', 'TRDT': 'TIME', ...}

with open(csv_file_full, 'w') as f:  # Just use 'w' mode in 3.x
    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)  # will write DATE, TIME, ... in correct place
            header_present = True


        w.writerow(my_dict)



回答2:


Yes, you can do like this: However ":" is not supported in filenames so 20171010–10.00.00

>>> import time
>>> fname = lambda : "mycsvfile{}.csv".format(time.strftime("%Y%m%d-%H.%M.%S"))
>>> 
>>> fname()
'mycsvfile20171012-17.24.59.csv'

>>> with open(fname()) as f:
>>>    pass    



回答3:


Have a variable for file name as file_name and use datetime.now()

from datetime import datetime
file_name = 'mycsvfile' + str(datetime.now()) + '.csv'


来源:https://stackoverflow.com/questions/46705867/how-to-create-a-filename-with-the-current-date-and-time-in-python-when-query-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!