To read all files under "/ path / to".
import glob
for file in glob.glob('/path/to/*'):
f = open(file, 'r')
for line in f.readlines():
print line
f.close()
import datetime
today = datetime.datetime.today()
this_monday = today - datetime.timedelta(today.weekday())
this_sunday = today + datetime.timedelta(6 - today.weekday())
print 'Today: ' + today.ctime()
print 'Monay: ' + this_monday.ctime()
print 'Sunday: ' + this_sunday.ctime()
Result
Today: Fri Jun 19 13:20:59 2015
Monay: Mon Jun 15 13:20:59 2015
Sunday: Sun Jun 21 13:20:59 2015
For example, convert a 1-digit number to a 4-digit character string and fill the left with 0.
num = 1
('0' * 4 + str(num))[-4:]
Result
'0001'
>>> a = ['a', 'b', 'c']
>>> b = [1,2,3]
>>> dict(zip(a,b))
{'a': 1, 'c': 3, 'b': 2}
in json.dump
sort_keys=True
Just pass.
>>> import json
>>>
>>> d = {'b':1, 'a':2, 'c':3}
>>>
>>> json.dumps(d)
'{"a": 2, "c": 3, "b": 1}'
>>>
>>> json.dumps(d, sort_keys=True)
'{"a": 2, "b": 1, "c": 3}'
>>>
Remove the comma with replace.
>>> '5,007,167,488'.replace(',','')
'5007167488'
>>> int('5,007,167,488'.replace(',',''))
5007167488
Recommended Posts