SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def csv_to_json(csv_file):
  2.     # open the file for reading and collecting header
  3.     try:
  4.         file = open(csv_file, "r")
  5.     except Exception as err:
  6.         logger.error(f"Got an error while trying to open {str(csv_file)}")
  7.         logger.error(f"ERROR: {str(err)}")
  8.         exit(1)
  9.     file_list = file.readlines()
  10.     # In case that the file has less then two lines, there is not enough data for performing the task (first line is the header)
  11.     if len(file_list) < 2:
  12.         logger.error("The file should have at least 2 lines (header and data)")
  13.         logger.error(f"This file has only {len(file_list)}")
  14.  
  15.     header = file_list[0].replace(', ', ',').replace(' ,', ',').rstrip('\n').split(',')  # remove spaces from each field
  16.     logger.debug("The header of the file is: " + str(header))
  17.     file.close()
  18.  
  19.     # open the file again in order to add everything beside the header (only content)
  20.     try:
  21.         file = open(f"{csv_file}_tmp", "w+")
  22.     except Exception as err:
  23.         logger.error(f"Got an error while trying to open {str(csv_file)}")
  24.         logger.error(f"ERROR: {str(err)}")
  25.         exit(1)
  26.     file_list_no_header = file_list[1:]
  27.  
  28.     # removing spaces from each field of the file
  29.     new_file_list_no_header = []
  30.     for line in file_list_no_header:
  31.         new_file_list_no_header.append(line.replace(', ', ',').replace(' ,', ','))
  32.  
  33.     file.writelines(new_file_list_no_header)  # add the clean fields to the file
  34.     file.close()
  35.  
  36.     # check if there are lines that do not correspond with the number of columns in the header (should have the same size)
  37.     header_size = len(header)
  38.     for row in new_file_list_no_header:
  39.         row = row.split(',')
  40.         row_size = len(row)
  41.         if row_size > header_size or row_size < header_size:
  42.             logger.error("One of the rows has more columns then the header, please fix it!")
  43.             logger.error(f'The header has {header_size} and the row column size is {row_size}')
  44.             logger.error(f"Header: {header}")
  45.             logger.error(f"Row: {row}")
  46.             exit(1)
  47.  
  48.     # open the file in order to read it in the CSV reader
  49.     try:
  50.         file = open(f"{csv_file}_tmp", "r")
  51.     except Exception as err:
  52.         logger.error(f"Got an error while trying to open {str(csv_file)}")
  53.         logger.error(f"ERROR: {str(err)}")
  54.         exit(1)
  55.     reader = csv.DictReader(file, fieldnames=header)
  56.     device_json = json.dumps([row for row in reader])
  57.     device_json = json.loads(device_json)
  58.     logger.debug("Devices parsed from CSV file: " + str(device_json))
  59.     file.close()
  60.  
  61.     logger.info("Devices found:" + str(len(device_json)))
  62.     os.remove(f"{csv_file}_tmp")  # delete the tmp file (the file without the header)
  63.  
  64.     return device_json
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top