Python file manipulation and student management system exercises

Posted Jun 28, 20203 min read

File reading

f=open('index.txt',encoding='utf-8')
s = f.read()
print(s)
f.close()

File writing

f = open('index.txt',mode="w",encoding='utf-8')
f.write('Call me Zhan Hiding\\n')
f.write('Call me Zhan Hiding 1\\n')
f.close()

Install and use third-party libraries

import random
random.randint() random number
import jieba stammer
import wordcloud word cloud

jieba.lcut('Sequence to split Chinese words') Sequence to split Chinese words
word\_cloud = wordCloud(font\_path='msyh.ttc').generate('Split Chinese words')#Generate word cloud object
word\_cloud.to\_file('123.png')#Save to picture

Classes and objects in python

Object-Oriented Programming
class Person:
 def \_\_init\_\_(self,name,sex,birthday):
 self.name = =name
 self.sex=sex
 self.birthday=birthday
 def  say(self,word):
 print(f'{self.name} says:"{word}"')

zhang\_san =Person('Zhang San','Male','2020202')
zhang\_san.say('12121')

Student Management System

1. Display all student information
2. New student information
3. Query student information
4. Modify student information
5. Delete student information
0. Exit the system

student\_data \= \[
 {
 'id':123456,
 'name':'Tom',
 'sex':'Male',
 'address':'Disney'
 },
 {
 'id':123457,
 'name':'Jerry',
 'sex':'Female',
 'address':'London'
 },
\]

Beautify the display

def beauty\_list(datas):
 for index, student in enumerate(datas):
 print(f'serial number:{index}', end\="\\t")
 print(f'Name:{student\["name"\]}', end\="\\t")
 print(f'Gender:{student\["sex"\]}', end\="\\t")
 print(f'address:{student\["address"\]}')

Enter the name

def input\_name():
 while True:
 name \= input('input name:').strip()
 if name:
 return name
 else:
 continue

Choose gender

def choose\_sex():
 print('1(Male)|2(Female)')
 n \= input('select gender')
 if n \== '1':
 return'Male'
 else:
 return'Female'
  1. Display all student information

    def show_all():
    beauty_list(student_data)

  2. New student information

    def create_student():
    sid = random.randint(1000, 10000)
    name = input_name()
    sex = choose_sex()
    address = input('Address:')
    student = {
    'id':sid,
    'name':name,
    'sex':sex,
    'address':address
    }

    student_data.append(student)

  3. Query student information

    def find_student():
    name = input_name()
    for i in student_data:
    if i['name']== name:
    print(i)
    return
    else:
    print('No information for this student')

  4. Modify student information

    def edit_student():
    name = input_name()
    for student in student_data:
    if student['name']== name:
    print(student)
    student['name']= input_name()
    student['sex']= choose_sex()
    student['address']= input('address:')
    return
    else:
    print('Check for this person')

  5. Delete student information

    def delete_student():
    name = input_name()
    for student in student_data:
    if student['name']== name:
    student_data.remove(student)
    return
    else:
    print('Check for this person')

    while True:
    print('''
    ********************
    Welcome to the student management system

    1. Display all student information

    2. New student information

    3. Query student information

    4. Modify student information

    5. Delete student information

    6. Exit the system
      ********************

      '''
      )

      op = input('Please enter the serial number:')
      if op == '1':
      print(student_data)
      show_all()
      elif op == '2':
      create_student()
      elif op == '3':
      find_student()
      elif op == '4':
      edit_student()
      elif op == '5':
      delete_student()
      else:
      print('Exit system')
      break