Skip to content

Commit ad1cc40

Browse files
authored
[+] Added : New Script To Manage CSV For Storage
-> will allow the user to manage CSV files -> filter data based on certain conditions. -> update data at particular row in csv.
1 parent 0879d79 commit ad1cc40

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

Better_CSV_Storage/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
p
2+
3+
# Better CSV Storage
4+
5+
-> For User / Programmer who don't want to use databases and want to use csv instead, this script will help manage that csv file (storage)
6+
7+
8+
9+
### What you Can Do ?
10+
11+
-> Filter Data Based On Certain Conditions that you provide,
12+
13+
-> Update Certain Value in particular row.
14+
15+
16+
17+
### How to use ?
18+
19+
-> It's OOP based Script so just import that in you Regular Script.
20+
21+
-> For Example,
22+
23+
​ -> I want to check and download_status of files.
24+
25+
​ -> csv file i have used -> `dummy_data.csv`
26+
27+
```python
28+
csv_path = 'dummy_data.csv'
29+
30+
#init class and get the object
31+
csv_obj = BetterCSVStorage(csv_path)
32+
33+
#Now user that object as storage
34+
35+
#Filter Data Based on Different parameters
36+
#This List will contain additional ( update_index ) which is row index of each row in csv.
37+
#This update_index will be use to update value of certain index.
38+
filtered_data = csv_obj.get_filtered_data('download_status', '==', '')
39+
40+
#Change Data Based on Different parameters
41+
csv_obj.update_data(10,'download_status', 'done')
42+
```
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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')

Better_CSV_Storage/dummy_data.csv

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
id,file_name,file_url,download_date,download_status
2+
1430,sample_video1.mp4,https://example.com/download/sample_video1.mp4,05/10/22,
3+
239,sample_video2.mp4,https://example.com/download/sample_video2.mp4,05/10/22,
4+
3421,sample_video3.mp4,https://example.com/download/sample_video3.mp4,05/10/22,
5+
1305,sample_video4.mp4,https://example.com/download/sample_video4.mp4,05/10/22,
6+
6375,sample_video5.mp4,https://example.com/download/sample_video5.mp4,06/10/22,done
7+
1203,sample_video6.mp4,https://example.com/download/sample_video6.mp4,06/10/22,done

0 commit comments

Comments
 (0)