Reputation: 33
I was given a task to write a student database and I cannot use any libraries. One of the options is to add a new student, and I was wondering if there's a way to append that info without using the module? I searched up and down on google and I can't seem to find anything. I'm new to python so I'm sorry if it's a stupid question. Below is the function I would love to use in that project.
from csv import writer
def add_new():
new = [x for x in input("New student data: ").split()]
with open("students.csv","a", newline="") as f:
writer_object = writer(f)
writer_object.writerow(new)
f.close()
add_new()
Upvotes: 1
Views: 105
Reputation: 159
You can write the data to the file as follows using only standard library (however in this case there is no validation of the user input):
def add_new():
new = input("New student data: ")
with open("students.csv","a", newline="") as f:
f.write(new)
add_new()
Please note that because you are (correctly) using the "with" context handler, there is no more need for f.close(). The file closes automatically.
Upvotes: 1