[Python] Is there any way that we can take in data from a web app, store it into a csv file...

Discussão em 'Python' iniciado por Stack, Julho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I have just begun learning how to use python for web apps. I don't plan on using it but I was just curious and trying it. I'm trying to take student data like name,roll no, branch and course from html input elements and storing them in a csv file using python.

    My main.py file -


    import csv
    from pyscript import document

    def submit_form(event):
    name = document.querySelector('#name')
    enrollment = document.querySelector('#enrollment')
    branch = document.querySelector('#branch')
    course = document.querySelector('#course')
    store_data(name.value,enrollment.value,branch.value,course.value)
    name.value=""
    enrollment.value=""
    branch.value=""
    course.value=""

    def store_data(name,enr,branch,course):
    with open('student.csv','+a') as f:
    w = csv.DictWriter(f,fieldnames=\['Name','Enrollment No','Branch','Course'\])
    w.writerow({'Name':name,'Enrollment No':enr,'Branch':branch,'Course':course})


    This is the code I wrote. What's not happening or maybe it is and I'm not seeing it is that the student.csv file isn't being created. No errors are being thrown. I figured it was because I was trying to do file handling operations inside a function but then I tried it separately and file handling works even inside a function. So please help me understand what I am doing wrong.

    Continue reading...

Compartilhe esta Página