|
| 1 | +import csv |
| 2 | + |
| 3 | +class BetterCSVStorage(): |
| 4 | + |
| 5 | + def __init__(self, csv_path): |
| 6 | + self.csv_f_path = csv_path |
| 7 | + self.valid_headers = None |
| 8 | + self.allowed_comparison = ['==', '!='] |
| 9 | + |
| 10 | + def load(self): |
| 11 | + with open(self.csv_f_path, 'r') as csv_f: |
| 12 | + csv_reader = csv.reader(csv_f) |
| 13 | + self.valid_headers = next(csv_reader) |
| 14 | + |
| 15 | + def write_dict_csv(self, data_rows): |
| 16 | + if data_rows: |
| 17 | + field_names = list(data_rows[0].keys()) |
| 18 | + with open(self.csv_f_path,'w') as csv_wf: |
| 19 | + csv_writer = csv.DictWriter(csv_wf, fieldnames=field_names) |
| 20 | + csv_writer.writeheader() |
| 21 | + for row in data_rows: |
| 22 | + csv_writer.writerow(row) |
| 23 | + print('[+] Data Written Successfully ...') |
| 24 | + else: |
| 25 | + print('[-] Error : Data Rows Could not be empty ...') |
| 26 | + |
| 27 | + def get_filtered_data(self, col_name, comparison, value): |
| 28 | + if not self.valid_headers: |
| 29 | + self.load() |
| 30 | + if not col_name in self.valid_headers: |
| 31 | + print('[-] Error : Enter Valid Column Name.') |
| 32 | + print('[*] Info : Allowed Column Names Are : {}'.format(', '.join(self.valid_headers))) |
| 33 | + else: |
| 34 | + if not comparison in self.allowed_comparison: |
| 35 | + print('[-] Error : Invalid Comparison.') |
| 36 | + print('[*] Info : Allowed Comparison Are : {}'.format(', '.join(self.allowed_comparison))) |
| 37 | + else: |
| 38 | + filtered_data = [] |
| 39 | + with open(self.csv_f_path,'r') as c_file: |
| 40 | + csv_reader = csv.DictReader(c_file) |
| 41 | + for row_index, row in enumerate(csv_reader): |
| 42 | + try: |
| 43 | + if (comparison == '=='): |
| 44 | + if row[col_name] == value: |
| 45 | + row['update_index'] = row_index |
| 46 | + filtered_data.append(row) |
| 47 | + if (comparison == '!='): |
| 48 | + if row[col_name] != value: |
| 49 | + row['update_index'] = row_index |
| 50 | + filtered_data.append(row) |
| 51 | + except KeyError: |
| 52 | + continue |
| 53 | + return filtered_data |
| 54 | + |
| 55 | + def update_data(self, update_index, col_name, value): |
| 56 | + if not self.valid_headers: |
| 57 | + self.load() |
| 58 | + if not col_name in self.valid_headers: |
| 59 | + print('[-] Error : Enter Valid Column Name.') |
| 60 | + print('[*] Info : Allowed Column Names Are : {}'.format(', '.join(self.valid_headers))) |
| 61 | + else: |
| 62 | + if not update_index: |
| 63 | + print('[-] Error Valid Data Index ....') |
| 64 | + else: |
| 65 | + try: |
| 66 | + update_index = int(update_index) |
| 67 | + except: |
| 68 | + print('[-] Error : Update Index is Not Valid') |
| 69 | + return |
| 70 | + updated_rows = [] |
| 71 | + with open(self.csv_f_path,'r') as csv_rf: |
| 72 | + csv_reader = csv.DictReader(csv_rf) |
| 73 | + for row_index, row in enumerate(csv_reader): |
| 74 | + if update_index == row_index: |
| 75 | + print('[+] Updating Index {}'.format(update_index)) |
| 76 | + row[col_name] = value |
| 77 | + updated_rows.append(row) |
| 78 | + self.write_dict_csv(updated_rows) |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + |
| 82 | + csv_path = 'dummy_data.csv' |
| 83 | + |
| 84 | + #init class and get the object |
| 85 | + csv_obj = BetterCSVStorage(csv_path) |
| 86 | + |
| 87 | + #Now user that object as storage |
| 88 | + |
| 89 | + #Filter Data Based on Different parameters |
| 90 | + #This List will contain additional ( update_index ) which is row index of each row in csv. |
| 91 | + #This update_index will be use to update value of certain index. |
| 92 | + filtered_data = csv_obj.get_filtered_data('download_status', '==', '') |
| 93 | + |
| 94 | + #Change Data Based on Different parameters |
| 95 | + csv_obj.update_data(4,'download_status', 'done') |
0 commit comments