Advertisement
msoo248

Untitled

Dec 9th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. def read_file(name, separator=','):
  2.     csv_list = []
  3.     try:
  4.         with open(name, 'r') as csv_file:
  5.             number_of_collumns = len(csv_file.readlines())
  6.             for line in csv_file.readlines():
  7.                 if line.isspace(): continue
  8.                 assert line != number_of_collumns, "Your file is broken"              
  9.                 csv_list.append(line.strip().split(separator))    
  10.         return csv_list
  11.    
  12.     except FileNotFoundError:
  13.         print("File has not found. Please provide correct path")
  14.     except AssertionError as err:
  15.         print(err)
  16.        
  17.  
  18. def save_file(file, file_name, separator=','):
  19.     try:
  20.         with open(file_name+'_writed.csv','w+') as csv_file:
  21.             for line in file:
  22.                 csv_file.writelines("{}\n".format(separator.join(line)))
  23.     except IndexError:
  24.         print('Probably your list is empty.')
  25.  
  26.  
  27. def main(path):
  28.     readed_file = read_file(path)
  29.     save_file(readed_file,path)
  30.    
  31. if __name__ == '__main__':
  32.     main(path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement