Employee management system + character code + Python code file operation

Posted by afatkidrunnin on Wed, 10 Nov 2021 16:55:51 +0100

Employee management system + character code + Python code file operation

1. Employee management system

one point one   debug code debugging

1. First click on the left side of the code to be debugged with the left mouse button (a red dot will appear)
2. Right click debug to run the code

one point two   Employee management system

# 2.Define an empty list to store user dictionary data
user_data_list = []
# 1.After the code runs, you can cycle to prompt the user what function numbers can be selected

# 2.View specified user data(Format the output and beautify it)  
# 3.Modify user's salary(by knowing one method you will know all) Modify user name and age 
# 4.View all user data(for Circular display)
while True:
        1.Add user
        2.View user
        3.delete user
        4.Exit the system
    choice = input('Please enter the function number you want to perform>>>:')
    # Judge the number entered by the user
    if choice == '1':
        # 3.Get the user number first
        user_id = input('Please enter your number>>>:').strip()
        # 4.You should verify whether the user number already exists
        # 4.1.Loop to get each user dictionary
        for user_data_dict in user_data_list:  # {'user_id': '1', 'name': 'jason', 'age': '18'}
            if user_data_dict['user_id'] == user_id:
                print('User number already exists')
                break  # end for Cyclic check
            # 5.Get user related information
            name = input('Please enter your user name>>>:').strip()
            age = input('Please enter your age>>>:').strip()
            salary = input('Please enter your salary>>>:').strip()
            # 6.Construct a user dictionary
            tmp_dict = {}
            # 7.Create key value pairs
            tmp_dict["user_id"] = user_id
            tmp_dict["name"] = name
            tmp_dict["age"] = age
            tmp_dict["name"] = name
            # 8.Add to user list
            # 9.Prompt user to add successfully
            print('user:%s Added successfully' % name)
    elif choice == '2':
    elif choice == '3':
        print('Delete user function')
    elif choice == '4':
        print('Exit system function')
        print('Sorry, we don't have this feature option at the moment')


2. Character coding

  Character encoding is only related to files and strings, not video files, picture files, etc

What is character encoding?

Because the computer only recognizes binary, but users can see all kinds of language characters when using the computer

Character coding: data that internally records the corresponding relationship between human characters and numbers 0 and 1

3. Development history of character coding

1. One family dominates

The computer was originally invented by Americans in order to enable computers to recognize English characters

ASCII code: it records the corresponding relationship between English characters and numbers

One byte is the corresponding relationship. All English characters and symbols actually add up to no more than 127. The reason why eight bits are used is to find new languages later

There are two groups of correspondence that must be remembered: A-Z:65-90     a-z:97-122     0-9: 48 - 57

2. Separatist regime


In order to enable the computer to recognize Chinese, we need to invent another set of coding table GBK Code: it records the corresponding relationship between English and Chinese and numbers. For English, it still uses one byte, and for Chinese, it uses two or more bytes.

In fact, two bytes are not enough to indicate that all Chinese words may need more bits to represent
In order to enable computers to recognize Japanese, it is also necessary to invent a set of coding tables

  shift_JIS Code: records the corresponding relationship between Japanese and English and numbers


In order to enable the computer to recognize Korean, it is also necessary to invent a set of coding table Euc_kr Code: it records the corresponding relationship between Korean and English and numbers
3. Unification of the world


In order to realize the barrier free communication of text data between different countries, it is necessary to unify the coding


Unicode (universal code)


Use two or more characters to record the corresponding relationship between characters and numbers


Utf8 (optimized version of universal code)


English is still stored in one byte, and Chinese is stored in three or more bytes

PS: now the default code is utf8

4. Character coding practice

1. How to solve the problem of garbled documents

The file was compiled in what code, and it was solved in what code when it was opened

2. Coding differences caused by different versions of Python interpreter

The encoding used internally in Python 2. X defaults to ASCII

# file header coding:utf8

In python2, a small us = u 'you' should be added in front of the defined string

python3.X Internal use utf8,Customize file template content
file >>> settings >>> Editor >>> file and code templates >>> python script


3. Encoding and decoding
Convert human readable characters into numbers according to the specified encoding
Convert numbers into characters that human beings can understand according to the specified coding

     s = 'Don't think too much about it every day. It's over. Ollie, give it to me!!!'
res = s.encode('utf8')
print(res, type(res)) # bytes stay python This type can be directly regarded as binary data in
res1 = res.decode('utf8')


5. File operation

1. What is a document?

Files are actually shortcuts (interfaces) that the operating system exposes to users to operate the hard disk

2. How the code operates the file keyword open() in three steps:

1. Open the file with the keyword open 2. Operate the file with other methods 3. Close the file


What are the file paths?

1. Relative path     2. Absolute path

The combination of letters and slashes in the path has a special meaning. How to cancel it, add an R in front of the path string   ,  r'D:\py20\day08\a.txt'


res = open('a.txt', 'r', encoding='utf8')
res.close()  # Close file to free resources

  open(File path,read-write mode,Character encoding) File path and read / write mode are required, and character encoding is optional(Some patterns need coding)


with context management (can automatically help you close)

with open(r'a.txt','r',encoding='utf8') as f1:  # f1=open() f1.close()

6. File read / write mode

PS: complement grammatical structure has no actual meaning   pass  

r read only mode (can only be viewed and cannot be changed)

    path does not exist:Direct error reporting
    with open(r'b.txt', 'r', encoding='utf8') as f:
    Path exists
    with open(r'a.txt', 'r', encoding='utf8') as f:
        print(f.read())  # Read all the contents of the file
        f.write('123')  # Write file content

w write only mode (write only, no view)

     path does not exist:Path does not exist automatically created
     with open(r'b.txt', 'w', encoding='utf8') as f:
   Path exists:1.The contents of the file will be emptied first  2.Then perform the write operation
   with open(r'a.txt', 'w', encoding='utf8') as f:
    f.write('hello world!\n')
    f.write('hello world!\n')
    f.write('hello world!\n')

a append mode only (additional content)

a: if the path exists, the file pointer will be moved directly to the end of the file, and an empty document will be created when the file does not exist

 1 #3. Add content, write function, utf8 The encoding mode turns on the current path a.txt file
 2 res = open('a.txt', mode='a', encoding='utf8')
 3 # input hello Content, clear the previous, I want to insist on learning python!
 4 res.write(' world\n')
 5 res.write('python\n')
 6 # The file must be closed after operation to free up resources
 7 res.close()
 9 #result,stay a.txt Append to file world and python character
10  world
11 python


Small exercise (employee operating system)

 1 print('''The functions of the employee management system are as follows:
 2     1.Add user
 3     2.View user
 4     3.delete user
 5     4.Exit the system\n''')
 7 user_msg_list = []
 8 while True:
 9     # 1 Get user number
10     choice = input('Enter function number:').strip()
11     choice = int(choice)
12     # 2 Judge which function the user selects
13     if choice == 1:
14         # 3 Get the personal number entered by the user
15         user_id = input('Please enter your personal number:').strip()
16         # Determine whether the input is correct
17         for full_dict in user_msg_list:
18             if full_dict['user_id'] == user_id:
19                 print('This user number%s:Already exists'%user_id)
20                 break
21             else:
22                 pass
23         else:
24             # 4 Get user information
25             user_name = input('Please enter your name:').strip()
26             user_age = input('Please enter age:').strip()
27             user_salary = input('Please enter salary:').strip()
28             # 5.Create an empty dictionary
29             null_dic = {}
30             # 6.Add the information entered by the user to the empty dictionary
31             null_dic['user_id'] = user_id
32             null_dic['user_name'] = user_name
33             null_dic['user_age'] = user_age
34             null_dic['user_salary'] = user_salary
35             # 8. Add the input information from the dictionary to the list for query function query
36             user_msg_list.append(null_dic)
37             # 9.Prompt user to add successfully
38             print(f'user{user_name}Added successfully!')
39     elif choice == 2:
40         user_inp_id = int(input('User information query, enter the information to query id Number:').strip())
41         if user_inp_id != 1:
42             print('Entered id No registered user, unable to find relevant data')
43             continue
44         second_dic = user_msg_list[user_inp_id - 1]
45         second_list = second_dic.keys()
46         for i in second_list:
47             print(f'{i}:{second_dic[i]}')
49     elif choice == 3:
50         user_id = input('Press 3 to delete all data:').strip()
51         if user_id == 3:
52             del user_msg_list
53         print('All user data deleted')
54         if user_id != 3:
55             print('Instruction error, please enter 3 to delete')
57     elif choice == 4:
58         user_id = input('Press 4 to exit the system:').strip()
59         if user_id == 4:
60             print('Has exited the system')
61             exit()
62         if user_id != 4:
63             print('Command error, please enter 4 to exit the system')
64     else:
65         print('There is no current service for the time being')