reading and writing files
play

Reading and Writing Files February 25, 2017 1 Reading and Writing - PDF document

Reading and Writing Files February 25, 2017 1 Reading and Writing Files in Python 1.0.1 CS 0155 - Data Witchcraft - Spring 2017 1.0.2 Dr. Alexandros Labrinidis In [1]: # Encode a 5x5 character as a list of 5 rows letterA =


  1. Reading and Writing Files February 25, 2017 1 Reading and Writing Files in Python 1.0.1 CS 0155 - Data Witchcraft - Spring 2017 1.0.2 Dr. Alexandros Labrinidis In [1]: # Encode a 5x5 character as a list of 5 rows letterA = ['..x..','.x.x.','xxxxx','x...x','x...x'] # We expect . to be converted to a space character for row in letterA: # iterate over all rows in the list print (row) ..x.. .x.x. xxxxx x...x x...x In [2]: letterA = ['..x..','.x.x.','xxxxx','x...x','x...x'] for row in letterA: # iterate over all rows in the list for letter in row: # iterate over all characters in the string print (letter,end='') print () ..x.. .x.x. xxxxx x...x x...x In [3]: #letterA = ['..X..','.x.x.','xxxxx','x...x','x...x'] letterA = ['.XXX.','X...X','XXXXX','X...X','X...X'] for row in letterA: 1

  2. # iterate over all rows in the list for letter in row: # iterate over all characters in the string if (letter != '.'): print (letter, end='') else : # print . characters as spaces print (' ', end='') print () XXX X X XXXXX X X X X In [4]: # open file testfile for writing newfile = open('testfile',"w") for row in letterA: # write string row into testfile newfile.write(row) # make sure we switch to next line newfile.write(' \n ') newfile.close() In [5]: # open file testfile for reading fh = open('testfile','r') lili = fh.readlines() print (lili) for one in lili: print (one, end='') ['.XXX.\n', 'X...X\n', 'XXXXX\n', 'X...X\n', 'X...X\n'] .XXX. X...X XXXXX X...X X...X In [6]: # Recap letterA = ['..x..', '.x.x.', 'xxxxx', 'x...x', 'x...x'] print (letterA) 2

  3. ['..x..', '.x.x.', 'xxxxx', 'x...x', 'x...x'] In [7]: # Store letters in a dictionary all_letters = { 'a': ['..x..', '.x.x.', 'xxxxx', 'x...x', 'x...x']} In [8]: # READ IN all letters AND store in dictionary data structure # assume these are available in the current directory myletters = {} for letter in "abcdefghijklmnopqrstuvwxyz": #print ('LETTER',letter) filename = 'incoming/'+letter+'.txt' print ('FILENAME',filename) fh = open(filename,'r') myletters[letter] = fh.readlines() print (myletters[letter]) fh.close() print (myletters) FILENAME incoming/a.txt ['..x..\n', 'x...x\n', 'xxxxx\n', 'x...x\n', 'x...x\n', '\n'] FILENAME incoming/b.txt ['x...\n', 'x...\n', 'xxx..\n', 'x.x..\n', 'xx...\n'] FILENAME incoming/c.txt ['xxxx.\n', 'x...\n', 'x...\n', 'x...\n', 'xxxx.'] FILENAME incoming/d.txt ['****.\n', '*...*\n', '*...*\n', '*...*\n', '****. '] FILENAME incoming/e.txt ['****.\n', '*... \n', '****.\n', '*... \n', '****.\n'] FILENAME incoming/f.txt ['****.\n', '*...\n', '****.\n', '*... \n', '*... \n'] FILENAME incoming/g.txt ['xxxxx\n', 'x...\n', 'x.xxx\n', 'x...x\n', 'xxxxx'] FILENAME incoming/h.txt ['X...X\n', 'X...X\n', 'XXXXX\n', 'X...X\n', 'X...X\n'] FILENAME incoming/i.txt ['XXXXX\n', '..X..\n', '..X..\n', '..X..\n', 'XXXXX'] FILENAME incoming/j.txt ['..XXX\n', '...X.\n', '...X.\n', 'X..X.\n', '.XX..'] FILENAME incoming/k.txt ['X..X.\n', 'X.X..\n', 'XX...\n', 'X.X..\n', 'X..X.'] FILENAME incoming/l.txt ['X...\n', 'X...\n', 'X...\n', 'X...\n', 'XXXXX'] FILENAME incoming/m.txt ['.x.x.\n', 'x.x.x\n', 'x.x.x\n', 'x.x.x\n', 'x...x\n'] FILENAME incoming/n.txt ['x...x\n', 'xx..x\n', 'x.x.x\n', 'x..xx\n', 'x...x'] FILENAME incoming/o.txt 3

  4. ['.xxx.\n', 'x...x\n', 'x...x\n', 'x...x\n', '.xxx.'] FILENAME incoming/p.txt ['.xxx.\n', '.x.x.\n', '.xxx.\n', '.x...\n', '.x...'] FILENAME incoming/q.txt ['***..\n', '*.*..\n', '***..\n', '..*..\n', '..**. \n'] FILENAME incoming/r.txt ['***..\n', '*..*.\n', '*..*.\n', '***..\n', '*..*.\n'] FILENAME incoming/s.txt ['xxxxx\n', 'x...\n', 'xxxxx\n', '...x\n', 'xxxxx'] FILENAME incoming/t.txt ['xxxxx\n', '..x..\n', '..x..\n', '..x..\n', '..x..'] FILENAME incoming/u.txt ['...\n', 'x...x\n', 'x...x\n', 'x...x\n', 'xxxxx\n'] FILENAME incoming/v.txt ['x...x\n', 'x...x\n', '.x.x.\n', '.x.x.\n', '..x..'] FILENAME incoming/w.txt ['x...x\n', 'x...x\n', 'x.x.x\n', 'x.x.x\n', '..x..'] FILENAME incoming/x.txt ['x...x\n', '.x.x.\n', '..x..\n', '.x.x.\n', 'x...x'] FILENAME incoming/y.txt ['X...X\n', '.X.X.\n', '..X..\n', '..X..\n', '..X..'] FILENAME incoming/z.txt ['XXXXX\n', '...X.\n', '..X..\n', '.X...\n', 'XXXXX'] {'a': ['..x..\n', 'x...x\n', 'xxxxx\n', 'x...x\n', 'x...x\n', '\n'], 'b': ['x...\n', In [9]: # same as before, but now eliminate \n myletters = {} for letter in "abcdefghijklmnopqrstuvwxyz": #print ('LETTER',letter) filename = 'incoming/'+letter+'.txt' print ('FILENAME',filename) fh = open(filename,'r') lili = fh.readlines() #print (lili) myletters[letter] = [] for onestring in lili: #http://stackoverflow.com/questions/275018/how-can-i-remove-chomp-a-newline-in-python newstring = onestring.rstrip() #print (newstring, end='___ ') if (newstring): myletters[letter].append(newstring) print (myletters[letter]) fh.close() print (myletters) FILENAME incoming/a.txt ['..x..', 'x...x', 'xxxxx', 'x...x', 'x...x'] 4

  5. FILENAME incoming/b.txt ['x...', 'x...', 'xxx..', 'x.x..', 'xx...'] FILENAME incoming/c.txt ['xxxx.', 'x...', 'x...', 'x...', 'xxxx.'] FILENAME incoming/d.txt ['****.', '*...*', '*...*', '*...*', '****.'] FILENAME incoming/e.txt ['****.', '*...', '****.', '*...', '****.'] FILENAME incoming/f.txt ['****.', '*...', '****.', '*...', '*...'] FILENAME incoming/g.txt ['xxxxx', 'x...', 'x.xxx', 'x...x', 'xxxxx'] FILENAME incoming/h.txt ['X...X', 'X...X', 'XXXXX', 'X...X', 'X...X'] FILENAME incoming/i.txt ['XXXXX', '..X..', '..X..', '..X..', 'XXXXX'] FILENAME incoming/j.txt ['..XXX', '...X.', '...X.', 'X..X.', '.XX..'] FILENAME incoming/k.txt ['X..X.', 'X.X..', 'XX...', 'X.X..', 'X..X.'] FILENAME incoming/l.txt ['X...', 'X...', 'X...', 'X...', 'XXXXX'] FILENAME incoming/m.txt ['.x.x.', 'x.x.x', 'x.x.x', 'x.x.x', 'x...x'] FILENAME incoming/n.txt ['x...x', 'xx..x', 'x.x.x', 'x..xx', 'x...x'] FILENAME incoming/o.txt ['.xxx.', 'x...x', 'x...x', 'x...x', '.xxx.'] FILENAME incoming/p.txt ['.xxx.', '.x.x.', '.xxx.', '.x...', '.x...'] FILENAME incoming/q.txt ['***..', '*.*..', '***..', '..*..', '..**.'] FILENAME incoming/r.txt ['***..', '*..*.', '*..*.', '***..', '*..*.'] FILENAME incoming/s.txt ['xxxxx', 'x...', 'xxxxx', '...x', 'xxxxx'] FILENAME incoming/t.txt ['xxxxx', '..x..', '..x..', '..x..', '..x..'] FILENAME incoming/u.txt ['...', 'x...x', 'x...x', 'x...x', 'xxxxx'] FILENAME incoming/v.txt ['x...x', 'x...x', '.x.x.', '.x.x.', '..x..'] FILENAME incoming/w.txt ['x...x', 'x...x', 'x.x.x', 'x.x.x', '..x..'] FILENAME incoming/x.txt ['x...x', '.x.x.', '..x..', '.x.x.', 'x...x'] FILENAME incoming/y.txt ['X...X', '.X.X.', '..X..', '..X..', '..X..'] 5

  6. FILENAME incoming/z.txt ['XXXXX', '...X.', '..X..', '.X...', 'XXXXX'] {'a': ['..x..', 'x...x', 'xxxxx', 'x...x', 'x...x'], 'b': ['x...', 'x...', 'xxx..', In [10]: print (myletters['c']) ['xxxx.', 'x...', 'x...', 'x...', 'xxxx.'] In [11]: print (myletters['F'.lower()]) ['****.', '*...', '****.', '*...', '*...'] In [12]: print (myletters['f'.lower()]) ['****.', '*...', '****.', '*...', '*...'] In [13]: print (myletters['='.lower()]) # This breaks -- showcases what happens when key is not in dictionary --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-13-0ccaf12f4fa6> in <module>() ----> 1 print (myletters['='.lower()]) 2 # This breaks -- showcases what happens when key is not in dictionary KeyError: '=' In [14]: #specify a default value for when the character is unknown asterisk = ['*****','*...*','*.*.*','*...*','*****'] # get_pattern function definition def get_pattern(aletter): # convert all important characters into lower-case # http://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary if aletter.lower() in myletters.keys(): return myletters[aletter.lower()] else : return asterisk In [15]: print (get_pattern('f')) 6

  7. ['****.', '*...', '****.', '*...', '*...'] In [16]: print (get_pattern('F')) ['****.', '*...', '****.', '*...', '*...'] In [17]: print (get_pattern('bob')) # guaranteed to fail ['*****', '*...*', '*.*.*', '*...*', '*****'] In [18]: # FUNCTION to print only one letter at a time (all rows) def print_one (aletter): for row in get_pattern(aletter): for one in row: if (one != '.'): print ('x', end='') else : print (' ', end='') print () In [19]: print_one('x') x x x x x x x x x In [20]: print_one('v') x x x x x x x x x In [21]: print_one('V') x x x x x x x x x 7

  8. In [22]: print_one('@') xxxxx x x x x x x x xxxxx In [23]: # FUNCTION to print entire word def print_word(myword,cnt): for oneline in range(1,6): for i in range(cnt): for oneletter in myword: print_one_line(oneletter,oneline,cnt) print() In [24]: print (get_pattern('a')) print (get_pattern('a')[1]) ['..x..', 'x...x', 'xxxxx', 'x...x', 'x...x'] x...x In [25]: # function ro print one line at a time. def print_one_line(aletter, aline, cnt): #print ('aletter='+aletter+' / aline='+str(aline)) row = get_pattern(aletter)[aline-1] #print ('row='+row) for one in row: if (one != '.'): # if not a . then print an x print ('x'*cnt, end='') else : # if dot is used, we should print space instead print (' '*cnt, end='') print (' ',end='') In [26]: print_one_line('a',1,2) xx In [27]: print_word('c',1) xxxx x x x xxxx 8

Recommend


More recommend