读取输入文件并写入输出文件-Python [英] Reading input files and writing into output files - Python

查看:273
本文介绍了读取输入文件并写入输出文件-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下信息的输入文件(input.txt):

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

  • 学生人数(第一行)
  • 考试成绩数(第二行)
  • 学生姓名和分数列表

所以文本文件看起来像这样

So the text file looks something like this

4
5

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

我正在尝试创建一个python程序,该程序将读取此信息,并将某些值(班级平均成绩,学生姓名,学生成绩等)输出到其他文件(output.txt)中.

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.

推荐答案

您将要使用《熊猫手册》 )为了这.

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.

最后,我代表SO社区表示欢迎!我知道您似乎一开始就很艰难(包括所有不赞成票的人),但不要因此而灰心.通读如何提出一个好问题,回来,并帮助我们一起学习!

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!

这篇关于读取输入文件并写入输出文件-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆