Skip to content
Advertisement

Python – read txt file into list – display contents of the new list

I did ask this before on a different account but i lost the account and didnt see most comment or answers people gave so i have asked on this older account of mine

Im new to python and programming in general so i am not understanding what i should be doing to get the output that is expected. I think i got the read_file function correct but im not sure.

I have been looking at this for a while and im no closing to understanding how exactly to do this part.

The goal here is to display profiles from a text file but in a specific way.

the rules are:

JavaScript

here are some descriptions i have been given:

JavaScript

the expected output is:

JavaScript

The text is being read from a text file that looks like this:

JavaScript

I have made this so far:

JavaScript

the import profile is referring to this file:

JavaScript

Im so confused by this. Would anyone be able to explain it or give any pointers.

Ive tried to make this question as easy to understand as i could.

Thanks.

Advertisement

Answer

Updated code with both read_file() and display_summary() function

Output is the same as before. Packaged the code into two functions per the original requirement.

Please review this and see how to implement it in your code.

JavaScript

Review of your code:

1. read_file() function:

The ask: This function takes a file name and reads the contents of that file into the profile_list (list) passed as a parameter into the function. The function returns the list of profile objects. You must use a loop in your solution. You may use String and/or List methods in this function only. You may find the String methods split() and strip() useful here.

Let’s review your read_file() function. Below is your code.

JavaScript

Review comments:

  1. The function receives the filename (string) and profile_list (empty list). Why are you not using the filename to open the file?
  2. profile_list = [] – good job in resetting the list.
  3. filename = open ("profiles.txt", "r") -> you are required to use the name of the file passed in the function parameter. You should rewrite this as f1 = open(filename, 'r')
  4. for loop is good. change it to for line in f1:
  5. stripped_line = line.strip() -> good
  6. profile_list = stripped_line.split() -> why do you not taking advantage of splitting each line? Also you cannot store it into profile_list. This is your final list into which all lines need to be stored.
  7. Split the line into a temp variable. Then check if (temp[-1] == 'm') or (temp[-1] == 'f'). If so, then that line has profile name, email id, and profile sex.
  8. If temp does not meet this criteria, then its safe to store the data as is into the profile_list list.

Updated code with fix to read_file(filename, profile_list) function

JavaScript

Working code to print Profile Summary

Here’s the code that will print the Profile Summary. Look at this code and compare it with yours.

Tomorrow, I will review your code and provide you recommendations to improve code. If you can review the below code and see how I have done it, it may help you understand the processing logic.

JavaScript

The output of this will be:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement