Lorsque j'exécute ma requête ci-dessous, il crée un fichier appelé 'mycsvfile'. Toutefois, existe-t-il un moyen d'ajouter la date et l'horodatage en cours lors de la création du fichier CSV? Par exemple, si je lance cette requête maintenant, le fichier doit être nommé mycsvfile20171012 - 10:00:00 (quelque chose comme ça).Comment créer un nom de fichier avec la date et l'heure actuelles en python lorsque la requête est exécutée
Quelqu'un pourrait-il éditer mon code et me montrer comment faire s'il vous plaît?
Mon 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)
Merci à l'avance!
Jetez un oeil à la ([ 'DateTime'] https://docs.python.org/3/library/datetime.html# module-datetime), en particulier les fonctions ['datetime.now()'] (https://docs.python.org/3/library/datetime.html#datetime.datetime.now) et ['datetime.strftime() '] (https://docs.python.org/3/library/datetime.html#datetime.datetime.strftime). – mhawke