-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·36 lines (31 loc) · 1.74 KB
/
main.py
File metadata and controls
executable file
·36 lines (31 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import csv
import os
from config import *
def process_csv(input_file, output_file):
with open(input_file, 'r', newline='') as infile, open(output_file, 'w', newline='') as outfile:
reader = csv.DictReader(infile)
fieldnames = [field for field in reader.fieldnames if config.get(field, False)]
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for row in reader:
if all(row[key] for key, value in config.items() if value):
if phone_filter_include != []:
if any(row.get('phone', '').startswith(prefix) for prefix in phone_filter_include):
filtered_row = {key: value for key, value in row.items() if key in fieldnames}
writer.writerow(filtered_row)
elif phone_filter_exclude != []:
if not any(row.get('phone', '').startswith(prefix) for prefix in phone_filter_exclude):
filtered_row = {key: value for key, value in row.items() if key in fieldnames}
writer.writerow(filtered_row)
else:
filtered_row = {key: value for key, value in row.items() if key in fieldnames}
writer.writerow(filtered_row)
def process_folder(input_directory, output_directory):
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for file_name in os.listdir(input_directory):
if file_name.endswith('.csv'):
input_file_path = os.path.join(input_directory, file_name)
output_file_path = os.path.join(output_directory, file_name)
process_csv(input_file_path, output_file_path)
process_folder(input_folder, output_folder)