Skip to content

Commit

Permalink
csv file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshlinux committed May 17, 2020
1 parent 9344012 commit 3cf8dd9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 7 deletions.
15 changes: 15 additions & 0 deletions fileHandling/csv Files/AddRecord.py
Original file line number Diff line number Diff line change
@@ -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 ...")
24 changes: 24 additions & 0 deletions fileHandling/csv Files/deleteRecord_readerWriter.py
Original file line number Diff line number Diff line change
@@ -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")
14 changes: 14 additions & 0 deletions fileHandling/csv Files/search_record_reader.py
Original file line number Diff line number Diff line change
@@ -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....')
7 changes: 1 addition & 6 deletions fileHandling/csv Files/tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -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()

2 changes: 1 addition & 1 deletion student.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions update_readerWriter.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 3cf8dd9

Please sign in to comment.