Python: format()

In Python3, using format() for string formatting is standard instead using traditional “%” formatting. ‘FOO{0}BAR’.format(10) >>’FOO10BAR’ # parameters can be indexed or named ‘FOO{0}{1}{2}BAR’.format(‘a’, ‘b’, ‘c’) >>’FOOabcBAR’ ‘FOO{a}{b}{c}BAR’.format(a=1, b=2, c=3) >>’FOO123BAR’ # with separater ‘FOO{0:,d}BAR’.format(1000000) >>’FOO1,000,000BAR’ https://docs.python.org/2.7/library/stdtypes.html#str.format >This method of…

Python: tempfile read

Before reading tempfile contents, execute “seek(0)” is needed to point beginning of the file. http://stackoverflow.com/questions/1202848/python-tempfile-temporaryfile-cannot-be-read-why # -*- coding:utf-8 -*- from tempfile import NamedTemporaryFile from os import SEEK_END with NamedTemporaryFile() as f: #before seek(0) f.read() returns nothing f.write(‘Foo’) print f.read() >>…