0% found this document useful (0 votes)
32 views13 pages

Employee Salary Update and File Operations

Uploaded by

sisvsbro195328
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views13 pages

Employee Salary Update and File Operations

Uploaded by

sisvsbro195328
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# a binary file emp.

dat contains employee info with


emp_id,name,designation,salary
# increase salary by 25 %
# total number of managers and total salary increased
import pickle
employee_data = [
[101, "John Smith", "Manager", 75000],
[102, "Alice Johnson", "Developer", 60000],
[103, "Michael Davis", "Manager", 80000],
[104, "Emily Brown", "Designer", 55000],
[105, "Christopher Lee", "Analyst", 65000],
[106, "Sophia Miller", "Manager", 78000],
[107, "Daniel Wilson", "Tester", 50000],
[108, "Olivia Taylor", "Developer", 62000],
[109, "Matthew Anderson", "Manager", 82000],
[110, "Emma Harris", "Designer", 57000]
]
# writing data into [Link] as a individual list
fo = open("[Link]", "wb")
for i in employee_data:
[Link](i, fo)
[Link]()

updated_data = []
no_of_managers = 0
total_increased_sal = 0
fo = open("[Link]", "rb")
while True:
try:
emp_data = [Link](fo)
if emp_data[2] == "Manager":
no_of_managers += 1
sal = float(emp_data[3])
increased_sal = (0.25*sal)
total_increased_sal += increased_sal
sal += increased_sal
emp_data[3] = sal
updated_data.append(emp_data)
except:
print("all records read")
[Link]()
break
print("total number of managers ",no_of_managers)
print("total salary increased ",total_increased_sal)

fo = open("[Link]", "wb")
[Link](updated_data,fo)
[Link]()
Q1 Read a text file and display the number of vowels,
consonants, uppercase, lowercase characters in the file. Also find
the number of words ending with a vowel.

'''
1. Read a text file and display the number of vowels, consonants,
uppercase, lowercase characters in the
file. Also find the number of words ending with a vowel.
'''

vowel_count = 0
constant_count = 0
uppercase_count = 0
lowercase_count = 0
fo = open("[Link]", "r")
all_data = [Link]()
[Link]()

for i in all_data:
if i in "aeiouAEIOU":
vowel_count += 1
else:
constant_count += 1

if [Link]():
uppercase_count += 1
elif [Link]():
lowercase_count += 1

print("number of vowel character", vowel_count)


print("number of constant character", constant_count)
print("number of uppercase character", uppercase_count)
print("number of lowercase character", lowercase_count)

vowels_end_words = 0
all_words = all_data.replace('\n', " ").split(" ")
for word in all_words:
if word[-1] in "aeiouAEIOU":
vowels_end_words += 1
print("number of words ending with vowels", vowels_end_words)

Q2 Remove all the lines that contain the character ‘a’ in a file,
change the words starting with a vowel to upper case and write
the new content to another file.

'''
Remove all the lines that contain the character 'a' in a file, change the
words starting with a vowel to
upper case and write the new content to another file.
'''

fo=open("[Link]","r")
all_lines=[Link]()
[Link]()
filtered_lines=[]
for i in all_lines:
if 'a' not in i:
filtered_lines.append(i)

updated_words=[]
for line in filtered_lines:
for word in [Link]("\n"," ").split(" "):
print(word)
if len(word)> 0 and word[0] in "aeiouAEIOU":
word=word[0].upper()+word[1:]
updated_words.append(word)

new_text=" ".join(updated_words)

fo = open("test_output.txt", "w")
[Link](new_text)
[Link]()
Q3 Write a program to create a CSV file with n doctor names,
fees and patient names. Input a doctor name and display all the
patient names for the doctor name entered by the user, if not
found display appropriate message.

# Write a program to create a CSV file with n doctor names, fees and
patient names. Input a doctor name and display all the patient names for
the doctor name entered by the user, if not found display appropriate
message.
import csv
all_records = []
no = int(input("enter number of records: "))
for i in range(no):
dn = input("enter doctor name: ")
fs = input("enter the doctor fees: ")
pn = input("enter the patient name: ")
all_records.append([dn, fs, pn])

fo = open("[Link]", "w")
wo = [Link](fo)
[Link](all_records)
[Link]()

fo = open("[Link]", "r")
dn = input("enter doctor name to search it's patients : ")
patient_names = []
records = [Link](fo)
for r in records:
if r[0].lower() == [Link]():
patient_names.append(r[2])

if len(patient_names) == 0:
print("no patient found for the doctor ", dn)

else:
print(patient_names, " patient found for the doctor ", dn)
[Link]()

Q4 Write a program to create a CSV file for n students with roll


number, name and marks. Search for a roll number and display
the names whose marks are greater than 90.

# Write a program to create a CSV file for n students with roll number,
name and marks. Search for a roll number and display the names whose marks
are greater than 90.

import csv
all_records = []
no = int(input("enter number of records: "))
for i in range(no):
rn = input("enter roll number: ")
name = input("enter the name: ")
marks = input("enter the marks: ")
all_records.append([rn, name, marks])

fo = open("[Link]", "w")
wo = [Link](fo)
[Link](all_records)
[Link]()

fo = open("[Link]", "r")
rn = input("enter roll_number to search : ")
search = []
records = [Link](fo)
for r in records:
if r[0] == rn:
[Link]([r[1], r[2]])
if float(r[2]) > 90:
print("student found with marks > 90 ")
print("roll_no ", r[0], "name ", r[1])

if len(search) == 0:
print("no student found for roll_no ", rn)

else:
print(search, " found for the roll_no ", rn)

[Link]()
Q5 Write a program to create a CSV file by entering user-id and
password, read and search the password for given user id.

# Write a program to create a CSV file by entering user-id and password,


read and search the password for given user id.

import csv

all_records = []

no = int(input("enter number of records: "))

for i in range(no):

user_id = input("enter user_id: ")

password = input("enter password: ")

all_records.append([user_id, password])

fo = open("[Link]", "w")

wo = [Link](fo)

[Link](all_records)

[Link]()

fo = open("[Link]", "r")
user_id = input("enter user_id to search : ")

search = []

records = [Link](fo)

for r in records:

if r[0] == user_id:

[Link]([r[0], r[1]])

if len(search) == 0:

print("no cred found with user id ", user_id)

else:

print(search, " found for the user_id ", user_id)

[Link]()
Q9 Write a program to create a binary file [Link] assuming
the binary file is containing the records of the following type:
{"BookNo": "Book_name"}. Write a function in PYTHON to search
for a BookNo from a binary file “[Link]”. Assume that
BookNo is an integer.
# 9) or 3) Write a program to create a binary file [Link] assuming the
binary file is containing the records of the following type: {"BookNo":
"Book_name"}. Write a function in PYTHON to search for a BookNo from a
binary file “[Link]”. Assume that BookNo is an integer.

import pickle

def search_book():
fo = open("[Link]", "rb+")
book_info = [Link](fo)
[Link]()
book_number = int(input("enter your book_number to search: "))
book_name = book_info.get(
str(book_number), f"no book found with {book_number}")
print(book_name)

book_info = {}
n = int(input("enter number of books: "))
for i in range(n):
book_no = input("enter the book number: ")
book_name = input("enter the book name: ")
book_info[book_no] = book_name

fo = open("[Link]", "wb")
[Link](fo, book_info)
[Link]()

search_book()
Q11. Write a program to implement a stack to store phone
numbers and names of n employees with the following operations
(menu driven): push(), pop(), show_all()

Q12. Write a program to implement a stack to store phone


numbers and names of n employees with the following
operations: delete any(), insertafter(), show_all()

# 11. Write a program to implement a stack to store phone numbers and


names of n employees with the following operations (menu driven): push(),
pop(), show_all()

# 12. Write a program to implement a stack to store phone numbers and


names of n employees with the # following operations: delete_any(),
insert_after(), show_all()

stack = []

def push():
name = int(input("enter the name: "))
phone_number = int(input("enter the phone number: "))
[Link]([name, phone_number])

def pop():
if len(stack) <= 0:
print("stack is empty")
else:
ele = [Link]()
print("popped top most element of the stack: ")
print("name:", ele[0])
print("phone number:", ele[1])

def show_all():
if len(stack) <= 0:
print("stack is empty")
else:
for i in range(len(stack)-1, -1, -1):
ele = stack[i]
print("name:", ele[0])
print("phone number:", ele[1])

def delete_any():
index_position = int(input("enter the +ve index position: "))
if (index_position < 0 or index_position > (len(stack)-1)):
print("invalid index position")
else:
ele = [Link](index_position)
print("popped top most element of the stack: ")
print("name:", ele[0])
print("phone number:", ele[1])

def insert_after():
index_position = int(input("enter the +ve index position: "))
if (index_position < 0 or index_position > (len(stack)-1)):
print("invalid index position")
else:
name = int(input("enter the name: "))
phone_number = int(input("enter the phone number: "))
[Link](index_position, [name, phone_number])

while True:
print("Choose the stack operation")
print("1. push()")
print("2. pop()")
print("3. show_all()")
print("4. delete_any()")
print("5. insert_after()")
print("6. exit()")
print("enter your choice: ")
choice = int(input("enter you choice: "))
if choice == 1:
push()
elif choice == 2:
pop()
elif choice == 3:
show_all()
elif choice == 4:
delete_any()
elif choice == 5:
insert_after()

You might also like