search

Home  >  Q&A  >  body text

How to set a dynamic variable in Python that changes with system time?

Newbie, I am making a python program, or script, to process logs. After processing and inserting it into elastic search, how to set a dynamic variable that can change with the system time. For example, the variable is num0508 today and will become num0509 tomorrow?

巴扎黑巴扎黑2752 days ago1234

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-05-18 10:54:07

    import datetime
    >>> datetime.datetime.now().strftime("%Y-%m-%d)
    '2017-05-08'

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-18 10:54:07

    I guess you want to insert logs or data into an index named after date. If so, you can just use a string

    import time
    from elasticsearch import Elasticsearch
    
    index_name = 'num_%s' % time.strftime('%m%d', time.localtime())
    es = Es()
    es.index(index_name, ...(自己补充其他参数内容))

    If you explain your scenario more clearly, I believe we will provide a method that better meets your needs

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-18 10:54:07

    Follow the code in the picture below to implement it

    reply
    0
  • Cancelreply