current position:Home>Python basic problems, read and write files, and solve them in a basic way

Python basic problems, read and write files, and solve them in a basic way

2022-02-02 14:26:43 CSDN Q & A

 ######  The phenomenon and background of the problem

 ######  Problem related code , Do not paste screenshots

 ###### Operation results and error reporting contents

 ###### My solution ideas and tried methods

 ###### What I want to achieve
The overall goal of this exercise is :

  • Read the data in the file , And store in the form of patient list ; Each patient will be represented by a dictionary , With three keys :"name", "weight" and "height".

  • Calculate the... For each patient BMI( Body mass index ), And the average number of patients BMI; Test which patients are normal , Which are not .

  • Write the result to another file .

The first 1 part : Import data and put it into the correct data structure

Write a import_lignes function , This function takes the file name as an argument and returns the line list of the file , String list , Each element of the list corresponds to a line of the file . Remember to execute return Close the file before .

Write a function separe , It will be one line ( character string ) As a parameter , And return the dictionary corresponding to the patient in this line . So , Please use the separate provided to you ( That's ok ) Function and returns a list of three strings , The list is obtained by following the space separator . Before storing them in a dictionary , Consider converting weight and height to floating .
Example :
separe ("Liliane 62 1.67 \ n") return ['Liliane', '62', '1.67'].
separe( "Liliane 62 1.67 \ n" ) Must return {'name': 'Liliane', 'weight': 62.0, 'height': 1.67}
Write a list_patients_from_nom_fichier function , This function takes the file name as an argument , And return the patient list corresponding to the file ( Each of these patients is represented by a dictionary ).
The first 2 part : Processing data

Write a function imc , It will the patient ( Dictionaries ) As a parameter , And return to his BMI Round to two decimal places ( remember round (number, 2) The existence and function of ).BMI The calculation is as follows :BMI = weight /( Size 2).
Write a function imc_moyen , It takes the patient list as a parameter , And return the patient's average BMI, Round to two decimal places .
Write a list_names_patients_en_corpulence_normale function , This function takes the patient list as a parameter , And return to include BMI A list indicating the names of patients with normal obesity , in other words BMI Whether in 18.5 and 25 Between ( Include ).
The first 3 part : Write the results to a file

Warning :Caseine The operation of does not allow you to keep the changes made by the program to the file : At the end of the run or evaluation , Everything will be reset to zero . therefore , To test this part , You need to choose from the following options :

Or work in your spare time ( See the opening paragraph ), Then upload your submission
Or use For you display_console (file_name) function , This function allows you to display the contents of the file whose name is passed as a parameter to the console ( Please refer to the use example in the main program ).
Write function produire_chaine Accept the patient's parameters , The patient returns a string consisting of the patient's name , Followed by a space , Followed by... Rounded to two decimal places BMI, Followed by a newline .
Write a function ecrite_imc , It takes the name of the patient list and the target file as parameters , And write... In the file :
For every patient , One line contains their names , Then their BMI, Separate... With spaces ( Remember to use the production string function ),
And then we write “Average BMI:”, This is followed by the average of patients BMI, Round to two decimal places .
then , We write “ The name of a patient of normal size :”, And then go back , Then the names of patients with normal body size ( One name per line )
You can refer to data -results1.txt File to see an example of the expected results ( The data used in this example comes from data1.txt file ).
Write function traitement_complet_donnees Take the file name containing the data as the parameter , Then write the file name of the result and complete data processing ( in other words , He reads , Processing data , And write the results according to the instructions given in the previous question ). You can refer to results-data1.txt File to view the information about complete_data_processing (“data1.txt”、“data-results1.txt”) What should be produced by the call of .

Refer to the answer 1:

No data files ? Make it up by yourself ?
This topic , There should be training data ?

Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended