Reading input files and writing into output files - Python

2024/11/22 3:48:35

I have an input file (input.txt) with the following information:

  • Number of students (first line)
  • Number of test scores (second line)
  • list of student names and scores

So the text file looks something like this

4
5Jane Doe,80,75,90,100,95,68
Sara Jones,65,80,72,90,75,80
Bill Smith,50,70,90,70,55,90
John Foles,95,90,85,80,88

I am trying to create a python program that will read this information, and output certain values (class average score, student names, student scores, etc) into a different file (output.txt).

I've been working through it, and I can never get my program to do everything that I need. I am only able to, for example, output the class average only, or one student's score only. I can't figure out how to output more than one function.

I could really use some help.

Answer

You're going to want to use Pandas (See also Pandas Manual) for this.

First, copy the following to your clipboard:

Jane Doe,80,75,90,100,95,68
Sara Jones,65,80,72,90,75,80
Bill Smith,50,70,90,70,55,90
John Foles,95,90,85,80,88

Next, run the following script:

#%% Load Your Data
import pandas as pd
df = pd.read_clipboard(sep = ',')# YOU WILL HAVE TO LOAD YOUR ACTUAL DATA AS FOLLOWS:
# file_path = 'path/to/your/file.txt'
# df = pd.read_csv() number_of_students = df.shape[0]
number_of_tests = df.shape[1] -1  # This is the number of columns you have
score_names = ['score' + str(number+1) for number in range(number_of_tests)]
df.columns = ['student'] + score_names # Prepares the column names
df.set_index('student',inplace=True) # Makes the student names the index#%% Calculate Maximum, Minimum and Average Class Score per test    
score_summaries = df.describe()#%% Calulate the average score for all students across all tests
average_exam_score = df.mean().mean()#%% A single students' score
df.loc['Sara Jones']

The script will calculate a couple of your requests. One thing it doesn't do is load your your file (you'll have to remove the lines containing the number of students & test scores but no worries, it is recalculated from the score data itself). I've included a hint about how to do this in the comments and leave it to you to implement.

I encourage you to go through it and explore what removing some lines or changing others will do.

Finally, on behalf of the SO community I say WELCOME! I know it seems like you're off to a rough start (with all the downvotes and all) but don't let that discourage you. Read through How to ask a good question, come on back, and help us learn together!

https://en.xdnf.cn/q/120711.html

Related Q&A

Get strings list in python with regex [duplicate]

This question already has answers here:Split string with multiple-character delimiter(3 answers)Closed 6 years ago.I want extract strings from this text with regex:~ZCC0ZAF~World~AAEef~RZgthAD~AAjaKNed…

python static code analysis tools - code analysis (preliminary research question) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

how can I get all post of users in django [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python 3: Getting IndexError: list index out of range on shuffle method

Im building a blackjack command line game and Ive run into a snag. The shuffle feature on my deck class object keeps coming up with IndexError: list index out of range on line 29. It is a sporadic bug…

How to encrypt a file

Just trimmed this down big timeI have an overall assignment that must read a file, encrypt it and then write the encrypted data to a new file.what ive tried is this:filename=input("Enter file name…

Any Idea on how Should I analyze this Algorithm? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Syntax error ; multiple statements found while

Why is there this syntax error Multiple statements found while compiling a single statement given when I run this code? Answer and help will be super appreciated for this python newbie here

No module named PyPDF2._codecs, even after already installed

I have installed PyPDF2==2.3.0, but I still get the error below when I import PyPDF2. The error message is:ModuleNotFoundError: No module named PyPDF2._codecs

rearding regex (python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

While Loop Guessing Number Game - Python

Im trying to make a guess the number between 1-10 game but the while loops seems to keep running. I want to program to let the user guess a number then display if its too high or low etc then start aga…