diff --git a/fileHandling/csv Files/AddRecord.py b/fileHandling/csv Files/AddRecord.py new file mode 100644 index 0000000..0940960 --- /dev/null +++ b/fileHandling/csv Files/AddRecord.py @@ -0,0 +1,15 @@ +import csv + +rollno= int(input('Roll No :')) +name = input('Name :') +stream = input('Stream :') +fees = int(input('Fees :')) + +records = [rollno,name,stream,fees] + +f = open("student.csv", "a") +csvwriter = csv.writer(f, lineterminator='\n') +# csvwriter.writerow(header) +csvwriter.writerow(records) +f.close() +print("Record added ...") diff --git a/fileHandling/csv Files/deleteRecord_readerWriter.py b/fileHandling/csv Files/deleteRecord_readerWriter.py new file mode 100644 index 0000000..3332767 --- /dev/null +++ b/fileHandling/csv Files/deleteRecord_readerWriter.py @@ -0,0 +1,24 @@ +# program to update a record in csv file +import csv +name = input('Name to Delete :') +records = [] +found = 0 +file = open("student.csv", "r") +reader = csv.reader(file) +for record in reader: + if(record[1]!= name): + records.append(record) + found = 1 + +file.close() + +# Remove the old file and create a new csv file +file = open("student.csv", "w") +writer = csv.writer(file,lineterminator='\n') +writer.writerows(records) +file.close() + +if(found == 0): + print(name, " does not exists") +else: + print(name, " deleted successfully") diff --git a/fileHandling/csv Files/search_record_reader.py b/fileHandling/csv Files/search_record_reader.py new file mode 100644 index 0000000..4cfc91a --- /dev/null +++ b/fileHandling/csv Files/search_record_reader.py @@ -0,0 +1,14 @@ +import csv +name = input('Name to Search :') + +file = open('student.csv', 'r') +reader =csv.reader(file) +found = 0 +for x in reader: + if(x[1] == name): + print(name, ' found in CSV file..') + found = 1 +file.close() + +if(found == 0): + print(name, ' not found....') diff --git a/fileHandling/csv Files/tempCodeRunnerFile.py b/fileHandling/csv Files/tempCodeRunnerFile.py index 3dd3c29..8b13789 100644 --- a/fileHandling/csv Files/tempCodeRunnerFile.py +++ b/fileHandling/csv Files/tempCodeRunnerFile.py @@ -1,6 +1 @@ -import csv -f = open(r"C:\Users\rakesh\Desktop\student.csv", "r") -data = csv.DictReader(f) -for record in data: - print(record) -f.close() \ No newline at end of file + diff --git a/student.csv b/student.csv index 20d4d59..277c95e 100644 --- a/student.csv +++ b/student.csv @@ -2,4 +2,4 @@ rollno,name,stream,fees 12,surendra,Humanities,2356 13,Ashok Goyal,Humanities,2356 15,Nipun,Humanities,2356 -22,Ayush Negi,Science,2356 +22,Ayush Negi,Science,2356 \ No newline at end of file diff --git a/update_readerWriter.py b/update_readerWriter.py new file mode 100644 index 0000000..aa3ac5c --- /dev/null +++ b/update_readerWriter.py @@ -0,0 +1,28 @@ +# program to update a record in csv file +import csv +name = input('Name to Update :') +records = [] +found = 0 +file = open("student.csv", "r") +reader = csv.reader(file) +for record in reader: + if(record[1] == name): + record[1] = input('New Name:') + records.append(record) + found = 1 + else: + records.append(record) +file.close() + +# Remove the old file and create a new csv file +headers = ['rollno', 'name', 'stream', 'fees'] +file = open("student.csv", "w") +writer = csv.writer(file, lineterminator='\n') +writer.writerow(headers) +writer.writerows(records) +file.close() + +if(found == 0): + print(name, " does not exists") +else: + print(name, " updated successfully")