首页 > 编程语言 >Python教程 - 保存分数结果至文件

Python教程 - 保存分数结果至文件

时间:2022-08-21 16:45:32浏览次数:52  
标签:分数 教程 fs fns Python sum cols print txt

保存文件的方法

fns = 'test_out.txt'

with open(fns, 'w+') as fs:
    print('hello world by python', file=fs)

将上节课的分析的分数结果保存至文件

import tkinter
import tkinter.filedialog

fn = tkinter.filedialog.askopenfilename(filetypes=[('TXT','.txt')])

with open(fn, 'r') as f:
    lines = f.readlines()
    print(lines)


fns = fn.replace('.txt', '_out.txt')
print(fns)

with open(fns, 'w+') as fs:
    print('name', 'sum', 'avg', file=fs)
    for l in lines:
        if l.startswith('name'):
            continue
        cols = l.split()
        name = cols[0]
        sum = float(cols[1])+float(cols[2])+float(cols[3])
        print(name, sum, sum/3, file=fs)

标签:分数,教程,fs,fns,Python,sum,cols,print,txt
From: https://www.cnblogs.com/hgrun/p/python-savescores.html

相关文章

  • python 使用正则表达式截取字符串
    假设字符串“a={};”要截取包含花括号在内的内容importrepattern=r="=(.+?);"match_bet_list=eval(re.findall(pattern,match_bet_list,re.M)[0])re.M表示在字符......
  • python-%格式化输出
    输出输出使用的是print()函数,作用,将程序中的数据或结果打印到控制台(屏幕)print('helloworld')name='小明'print(name)age=18print(name,age)#可以使用逗号输出......
  • Python 处理html、url字符串编码和解码(base64,escape,urlencode)
    Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。对html进行编码Python2.x中可以使用cgi中escape,Pyth......
  • PS新手教程 --如何使用ps打造逼真冰冻水果效果
    如何使用ps打造逼真冰冻水果效果?水果加在冰块里的效果应该怎么做出来呢?用ps来做其实不难,让我来教教你!Photoshop2022 打开图片,我这里打开两张图片,做一下示范。1.先把......
  • 基于python的数学建模---logicstic回归
    樱花数据集的Logistic回归   绘制散点图importmatplotlib.pyplotaspltimportnumpyasnpfromsklearn.datasetsimportload_irisiris=load_iris()#获......
  • Github + Hexo 搭建个人博客超详细教程
    本文目录generatedwithDocToc网站搭建本文目录1.安装node.js2.添加国内镜像3.安装Git4.注册Github账号5.创建git仓库6.安装Hexo7.配置本地hexo8.连接Github......
  • Mac安装python jupyter notebook
    前置条件:已安装python3查看当前python版本:python--version如果不使用虚拟环境,直接用步骤3和步骤4即可。1.创建虚拟环境:pip3installvirtualenvpython3-mvirtuale......
  • python switch 替换if else
    1,python解释器版本3.10以上可以使用如下defdar(darling):matchdarling:case'400':print(400)case'401':print(4......
  • Python小游戏——外星人入侵(保姆级教程)第一章 05
    系列文章目录第一章:武装飞船05:重构:模块game_functions一、重构在大型项目中,经常需要在添加新代码前重构既有代码。重构旨在简化既有代码的结构,使其更容易扩展。在本节......
  • python 时间戳装饰器
    点击查看代码importtimefromfunctoolsimportwrapsdeftimer(func):@wraps(func)definner(*args,**kwargs):start=time.time()re......