Project File Class 12
Project File Class 12
Indian States
def add_record():
print("Add new record for the library database")
with open("population.csv", "a", newline="") as f:
s = csv.writer(f)
record = [int(input("Enter the serial number:\t")),
input("Enter the name of the state:\t"),
int(input("Enter the population of the state in 2021:\t")),
float(input("Enter the population decadal growth rate of the state:\t")),
int(input("Enter the population density of the state (per Sq. km):\t"))]
s.writerow(record)
print("Record saved!")
input("Press enter key to continue.\n")
def modify_record():
print("Modifying the data stored in the library database\n")
num = int(input("Enter the serial number of the data you want to modify:\t"))
display_csv_table(num)
if input("Is this the data you want to modify?:\t").lower() == "yes":
delete_record(str(num))
add_record()
print("The new data was updated\t")
input("Press enter key to continue\t")
def search():
print("Searching the library database\n")
num = int(input("Enter the serial number of the State you are searching for:\t"))
display_csv_table(num)
if input("Is this the data you were looking for?\t").lower() != "yes":
search()
input("Press enter key to continue\t")
def display_csv_table(row_index=None):
with open("population.csv", "r") as file:
reader = csv.reader(file)
table = list(reader)
max_lengths = [max(len(str(item)) for item in column) for column in zip(*table)]
total_width = sum(max_lengths) + 4 * (len(max_lengths) - 12)
if row_index is not None:
variables_row = table[0]
formatted_variables = [str(variable).ljust(max_length + 2) for variable, max_length in
zip(variables_row, max_lengths)]
 ̄
print(" " * total_width)
print('|' + '|'.join(formatted_variables) + '|')
 ̄
print(" " * total_width)
row = table[row_index]
formatted_row = [str(item).ljust(max_length + 2) for item, max_length in zip(row,
max_lengths)]
print('|' + '|'.join(formatted_row) + '|')
 ̄
print(" " * total_width)
else:
print("Displaying all the records\n")
for row in table:
formatted_row = [str(item).ljust(max_length + 2) for item, max_length in zip(row,
max_lengths)]
 ̄
print(" " * total_width)
print('|' + '|'.join(formatted_row) + '|')
 ̄
print(" " * total_width)
input("\nPress enter key to continue")
def delete_record(num=None):
rows_to_delete = []
updated_rows = []
with open("population.csv", "r", newline="") as f:
s = csv.reader(f)
if num is None:
ask = "no"
print("Deleting the data stored in the library database\n")
num = input("Enter the serial number of the data you want to delete:\t")
else:
ask = "yes"
for i, row in enumerate(s):
if num in row:
if ask != "yes":
display_csv_table(int(num))
ask = input("Is this the data you want to delete?:\t").lower()
if ask == "yes":
rows_to_delete.append(i)
print(“Record Successfully Deleted”)
else:
updated_rows.append(row)
with open("population.csv", "w", newline="") as f1:
s1 = csv.writer(f1)
s1.writerows(updated_rows)
input("Press enter key to continue\t")
def front_page():
print("Hello, This is a population database of various states in India.")
while True:
print('''
Press 1 to add a new record.
Press 2 to modify an existing data.
Press 3 to delete an existing data.
Press 4 to search through the data.
Press 5 to view all existing files.
Press 6 to exit.\n''')
number = int(input("Choose what you would like to do today:\t"))
if number == 1:
add_record()
elif number == 2:
modify_record()
elif number == 3:
delete_record()
elif number == 4:
search()
elif number == 5:
display_csv_table()
elif number == 6:
print("Bye Bye User !")
break
else:
print("Invalid choice. Please choose a number between 1 and 6.")
front_page()
# Adding a new record to the csv file using python