Advertisement
Guest User

Untitled

a guest
Oct 21st, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. def open_file():
  2.    
  3.        
  4.  
  5.     while(True):
  6.         try:
  7.             file_name = str(input('Enter a file name: '))
  8.             file_obj = open(file_name,'r')
  9.             L = []
  10.                
  11.             break
  12.            
  13.         except FileNotFoundError:
  14.             print('Error. File not found.')
  15.        
  16.     for file_line in file_obj:
  17.         L.append([float(x) for x in file_line.split(',')])
  18.            
  19.          
  20.     # make sure to close file_obj.close()
  21.  
  22.     index = 0
  23.     while(index < len(L)):
  24.         if(len(L[0]) == len(L[index])):
  25.             index+=1
  26.         else:
  27.             return('Data could not be loaded (the number of columns is not the same for all rows).')
  28.             break
  29.        
  30.        
  31.     print()
  32.     col_num = len(L[0])
  33.     row_index = 1
  34.     alpha_index = 0
  35.     alpha_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  36.     blank = ' '
  37.  
  38.     print('|{:^5}'.format(blank), end = '')
  39.        
  40.              
  41.     while(alpha_index < col_num):
  42.         print('|{:^10}'.format(alpha_str[alpha_index]), end = '')
  43.         alpha_index +=1
  44.        
  45.     print()
  46.  
  47.     for row in L:
  48.            
  49.         print('|{:^5}'.format(row_index), end ='')
  50.            
  51.         for elem in row:
  52.                
  53.             print('|{:^10.2f}'.format(elem),end = '')
  54.         row_index+=1  
  55.         print()
  56.  
  57.            
  58.  
  59.     print()
  60.        
  61.     return(L)
  62. def main():
  63.    
  64.     print('1 - Open and load from a file\n2 - Minimum\n3 - Maximum\n4 - Sum\n5 - Average\n6 - Sort (by column, ascending or decending)\n7 - Insert\n8 - Delete\n9 - Modify an element\n10 - Save\n11 - Save As (specify new file name)\n0 - Exit')
  65.    
  66.     while(True):
  67.        
  68.         task = (input('Your choice: '))
  69.         if(task == '0' or task == '1' or task == '2' or task == '3' or task == '4' or task == '5' or task == '6'
  70.         or task == '7'or task == '8'or task == '9' or task == '10' or task == '11'):
  71.  
  72.             break
  73.    
  74.     if(task == '0'):
  75.         pass
  76.     if(task == '1'):
  77.         numlist = open_file()
  78.         print(numlist)
  79.         main()
  80.    
  81.        
  82.  
  83. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement