# Python Input and Output Yuen Kwan Lo Output

• Slides: 12

Python: Input and Output Yuen Kwan Lo

Output Format str( ) and repr( ) same representation but String and Floating point number • a=0. 24 str(a) repr (a) ‘ 0. 24’ ‘ 0. 23999999999’ s = 'Hello, world. ' str(s) 'Hello, world. ' repr(s) "'Hello, world. '" print ( repr(s) ) ‘Hello, word. ’

rjust() , ljust(), center() � Returns string justified str = “hello world” print str. rjust (20) >> hello world print str. rjust(20, ‘!’) >> !!!!!hello world in a string of given length zfill() � Pads a numeric '-3. 14'. zfill(7) string on the left with zeros '-003. 14'

for x in range(1, 5): print repr(x). rjust(2), repr(x*x). rjust(3), print repr(x*x*x). rjust(4) format() for x in range(1, 5): print '{0: 2 d} {1: 3 d} {2: 4 d}'. format(x, x*x*x) print ‘PI : {0: . 3 f}'. format(math. pi) >> PI: 3. 142 � !s ( str() ) and !r ( repr() ) print ‘PI : {!r}'. format(math. pi) >> PI: 3. 141592653589793 print ‘PI : {!s}'. format(math. pi) >> PI: 3. 14159265359

� Print set of variables print '{1} and {0}'. format('spam', 'eggs') >> eggs and spam print '{dollar} dollars and {cent} cents'. format(dollar=‘ten’, cent=‘forty’) >> ten dollars and forty cents � Print table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678} for name, phone in table. items(): print '{0: 10} ==> {1: 10 d}'. format(name, phone) table = {'Sjoerd': 4127, 'Dcab': 8637678} print (‘Sjoerd: {0[Sjoerd]: d}; Dcab: {0[Dcab]: d}'. format(table)) >> Jack: 4098; Sjoerd: 4127; Dcab: 8637678 print 'Sjoerd: {Sjoerd: d}; Dcab: {Dcab: d}'. format(**table)

� String Formatting veg = “apple” color = “red” print veg + “ is “ + color print “%s is %s” % (veg, color) >> apple is red count= 6 print “There are %d %s” % (count, veg) >> There are 6 apple print “There are “ + count + veg ERROR!! � Numbers Formatting print "Today's stock price: %f" % 50. 4625 >> Today's stock price: 50. 462500 print "Today's stock price: %. 2 f" % 50. 4625 >> Today's stock price: 50. 46 print "Change since yesterday: %+. 2 f" % 1. 5 >> Change since yesterday: +1. 50

Reading and Writing Files open() � open(filename, mode) f = open('/cs/PA', 'w') mode: ‘r’ ‘w’ ‘a’ ‘r+’ ‘b’ read write *file with same name will be erased* append read and write open file in binary mode (on Windows)

Methods of File Objects � f. read(size) f. read(1) **read the entire file and return string** **read one character at a time** � f. readline() reads a single line from the file until it reaches ‘n’ � f. readlines() Read all lines and returns a list of all lines >>['This is the first line of the file. n', 'Second line of the filen']

� f. write(string) write the contents of string to the file f. write(‘first linen’) **make numbers s = str (0. 24) f. write(s) to string before writing** � f. tell() return current position in file measured in bytes from the beginning of the file � f. close() close file and file object cannot be used unless being reopened

� f. seek(offset, from: from) 0: beginning of file 1: current position 2: end of file f = open('/tmp/workfile', 'r+') f. write('0123456789 abcdef') f. seek(5) # Go to the 6 th byte in the file f. read(1) '5' f. seek(-3, 2) # Go to the 3 rd byte before the end f. read(1) 'd'

Pickle Module selfref_list = [1, 2, 3] output = open('data. pkl', 'wb') pickle. dump(selfref_list, output) data 1 = pickle. load(output) When reading a pickle-containing file, you should open the file in binary mode because you can't be sure if the ASCII or binary format was used

Reference � http: //docs. python. org/tutorial/inputoutput. html � http: //www. tutorialspoint. com/python/string_rjust. ht m � http: //diveintopython. org/native_data_types/formatti ng_strings. html � http: //docs. python. org/library/ � http: //en. wikibooks. org/wiki/Python_Programming/I nput_and_output � http: //docs. python. org/release/2. 5. 2/lib/pickleexample. html