-
Notifications
You must be signed in to change notification settings - Fork 0
/
35.py
82 lines (70 loc) · 2.03 KB
/
35.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Write a menu driven program to add and manipulate data
# from customer.csv file. Give function to do the following:
# 1.Add Customer Details, 2.Search Customer Details
# 3.Remove Customer Details
# 4.Display all the Customer Details, 5.Exit
import csv
def add():
with open('customer.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(["Cus_no", "C_name"])
n = int(input('Enter how many Customer you want to insert: '))
for _ in range(0, n):
cus_no = int(input('Enter Customer ID: '))
cus_name = input('Enter Customer Name: ')
info = [cus_no, cus_name]
writer.writerow(info)
def display():
with open('customer.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
def search():
data = []
with open('customer.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
data.append(row)
id = input('Enter customer ID to search info: ')
for i in data:
if i[0] == id:
print(f'Customer no. {i[0]} and Name is {i[1]}')
def remove():
data = []
new_data = []
with open('customer.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
data.append(row)
print(data)
name = input('Enter customer name to delete info: ')
with open('customer.csv', 'w', newline='') as f:
f.truncate(0)
for i in data:
if i[1] != name:
new_data.append(i)
writer = csv.writer(f)
writer.writerows(new_data)
k = True
while k == True:
print('''
1.Add Customer Details
2.Search Customer Details
3.Remove Customer Details
4.Display all the Customer Details
5.Exit
''')
option = int(input('Enter your option(1/5): '))
if option == 1:
add()
elif option == 2:
search()
elif option == 3:
remove()
elif option == 4:
display()
elif option == 5:
k = False
else:
print("Invalid Option!")
continue