[PYTHON] Prepared for date calculation and automation of my bot

Yes. In the json format that seemed to be the easiest to handle by myself because I got lost in various ways to do the title act.

test.json


[
{
   "name":"mouse",
   "date":"2013-10-22",
   "last":24,
   "avg":20.5,
   "cnt":10
}
]

test.py


from urllib import urlencode
from datetime import date, timedelta
import datetime
import kino
import re
import json

time = datetime.datetime.now()


def demchi_cnt(filename):
    f = open(filename)
    data = json.load(f)
    f.close()
    d = datetime.datetime.strptime(data[0]["date"], "%Y-%m-%d")
    s = time - d
    message = "@xxx After replacing the mouse battery%d days have passed. Last life%Average in d days%It's s day.#demchi" % (s.days,data[0]["last"],"{0:0.1f}".format(data[0]["avg"]))
#    kino.client.request('https://api.twitter.com/1.1/statuses/update.json', 'POST', urlencode({'status': message}))

if __name__ == '__main__':
    demchi_cnt(test.json)

At least the first time I need to prepare the data manually, so I checked here after inputting. http://jsonlint.com/

data[0]["last"]=20 After combing json.dump(a, open('test.json','w')) It seems that it will be reflected in the json file. Just like the elements are replaced in lexicographic order, trying a little more

Recommended Posts

Prepared for date calculation and automation of my bot
Pandas basics for beginners ④ Handling of date and time items
Derivation of EM algorithm and calculation example for coin toss
Calculation of homebrew class and existing class
Idempotent automation of Python and PyPI setup
[Python] Create a list of date and time (datetime type) for a certain period
Calculation of elapsed years and elapsed months including leap years
Calculation of technical indicators by TA-Lib and pandas
Calculation speed of indexing for numpy quadratic array