示例#1
0
 def read(self, filename):
     """
     Return dictionary with key => value pairs
     :param filename is the file where the values exist
     >>> f = FileTypeXLSX()
     >>> f.read("Saves/doctest.xlsx")
     {1: {'ID': 'G262', 'Gender': 'Female', 'Age': 12, 'Sales': 215, 'BMI': 'Normal', 'Salary': 23, 'Birthday': '24-05-1993'}, 2: {'ID': 'A233', 'Gender': 'Chihuahua', 'Age': 22, 'Sales': 245, 'BMI': 'normal', 'Salary': 23, 'Birthday': '24-06-1995'}, 3: {'ID': 'A262', 'Gender': 'M', 'Age': 24, 'Sales': 845, 'BMI': 'Normal', 'Salary': 23, 'Birthday': '24-05-1993'}, 4: {'ID': 'A233', 'Gender': 'Female', 'Age': 62, 'Sales': 245, 'BMI': 'normal', 'Salary': 23, 'Birthday': '24-06-1995'}}
     Adding Row 1
     {'ID': 'G262', 'Gender': 'F', 'Age': '12', 'Sales': '215', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-05-1993'}
     Error at entry: 2
     Adding Row 3
     {'ID': 'A262', 'Gender': 'M', 'Age': '24', 'Sales': '845', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-05-1993'}
     Adding Row 4
     {'ID': 'A233', 'Gender': 'F', 'Age': '62', 'Sales': '245', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-06-1995'}
     result
     {1: {'ID': 'G262', 'Gender': 'F', 'Age': '12', 'Sales': '215', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-05-1993'}, 3: {'ID': 'A262', 'Gender': 'M', 'Age': '24', 'Sales': '845', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-05-1993'}, 4: {'ID': 'A233', 'Gender': 'F', 'Age': '62', 'Sales': '245', 'BMI': 'Normal', 'Salary': '23', 'Birthday': '24-06-1995'}}
         """
     data = dict()
     empno = 0
     keys = []
     a_row = 0
     try:
         workbook = load_workbook(filename)
         first_sheet = workbook.sheetnames[0]
         worksheet = workbook[first_sheet]
         for row in worksheet.iter_rows():
             record = dict()
             row_num = 0
             for cell in row:
                 a_row = cell.row
                 if 1 == a_row:
                     keys.append(cell.value)
                 else:
                     valid = cell.value
                     if isinstance(cell.value, datetime):
                         valid = Validator.xlsx_date(cell.value)
                     record[keys[row_num]] = valid
                 row_num += 1
             if a_row > 1:
                 data[empno] = record
             empno += 1
         # print(data)
         result = Validator.save_dict(data)
         validator.a = None
     except PermissionError:
         result = dict()
         print("Sorry, you don't have enough permissions to access this file")
     print("result")
     return result
 def parse_data(self):
     result = dict()
     titles = self.file[0]
     row_num = 1
     for row in self.file:
         counter = 0
         if row is not self.file[0]:
             result_row = dict()
             for cell in row:
                 result_row[titles[counter]] = cell
                 if isinstance(cell, datetime):
                     cell = Validator.xlsx_date(cell)
                 result_row[titles[counter]] = cell
                 counter = counter + 1
             result[row_num] = result_row
             row_num = row_num + 1
     self.file = result
示例#3
0
 def test_date_object(self):
     expected = "05-05-1988"
     data = datetime.datetime(1988, 5, 5)
     result = Validator.xlsx_date(data)
     self.assertEqual(expected, result)