0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

自分用botの日付計算とか自動化の準備をした

Posted at

はい。
表題の行為をするのに色々迷走して自分で一番処理しやすそうだったjson形式で。

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 マウスの電池を交換してから%d日が経過しました。前回の寿命は%d日で平均%s日です。 #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)

最低でも一度目は手動でデータを用意する必要があるので入力後はここでチェックしました。
http://jsonlint.com/

data[0]["last"]=20
とかした後で
json.dump(a, open('test.json','w'))
でjsonファイルの方に反映されるっぽい。ただ要素が辞書順に置き換えられるような、、、もう少し色々試しながらで

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?