ADD file via upload

回归分析
hnu202111020216 4 years ago
parent 6304874489
commit c837e5ea64

@ -0,0 +1,172 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 29 16:15:40 2021
@author: Administrator
"""
s1="李联国"
print(s1.encode('utf-8'))
s2=s1.encode('utf-8')
print(s2.decode())#输出为李联国
#打开
f=open("score.txt","r+",encoding="utf-8")
print(f.read())
#f = open(file, mode, encoding=None)
#file表示要打开的文件路径相对或者绝对路径
#mode表示文件打开模式
#r+
#读写,会有输出
#w+
#消除原文件内容,然后写入新内容
#a+
#追加写入,如果该文件已存在,文件指针将会放在文件的结尾。不会有输出
#encoding用于设置编码格式一般使用utf-8
f.close()#关闭文本文件: f = close()
#在Python中为了防止文件打开后忘记关闭提供了with语句来使用文件
#with open(file, mode) as <文件对象名>: 语句块
with open("score1.txt","r+",encoding="utf-8") as s:
print(s.read())
#读取
with open("score.txt","r+",encoding="utf-8") as s:
print(s.read())#以字符串形式读取全部内容
s.seek(0)#把指针移回来0表示移到首位
print(s.readline())#以字符串形式读取整行
s.seek(0)
print(s.readlines())#读取所有行,每行作为列表中一个元素返回一个列表,会读取换行符\n
with open("score.txt","r+",encoding="utf-8") as s:#方法一
while True:
line=s.readline()
if line:#判断line是否为空
print(line,end="\n")
else:
break
with open("score.txt","r+",encoding="utf-8") as s:#方法二
lines=s.readlines()
print(lines)
for i in lines:
print(i,end="\n")
#将数据输出到文件
with open("test2.txt","w+",encoding="utf-8") as f:
str1="zhangsan 90\n"+"lisi 88\n"+"wangwu 78\n"
f.write(str1)#第一种方法:write([str]) write()方法用于向文件中写入一个字符串或字节流。
with open("test2.txt","r+",encoding="utf-8") as f:
print(f.read())
with open("test1.txt","a+",encoding="utf-8") as f:
L=["001 zhangsan 100\n","002 lisi 99\n"]
f.writelines(L)#第二种方法:writelines([str]) writelines()方法用于向文件中写入一个序列的字符串,如元素为字符串的列表。换行需要自己添加换行符“\n”。
with open("test1.txt","r+",encoding="utf-8") as f:
print(f.read())
with open("score.txt","r+",encoding="utf-8") as s:
lst1=s.readlines()
print(lst1)
for i in lst1:
x=i[0:3]
print(x)#姓名
i=i[3:]
i=i.split()
lst2=list(map(eval,i))
sum1=sum(lst2)
print(sum1)#总分
with open("totalscore.txt","a+",encoding="utf-8") as t:#把总分和姓名写入新的文件里
str1=f"{x},{sum1}\n"
print(str1)
t.write(str1)
with open("totalscore.txt","r",encoding="utf-8") as t:#读出新文件
print(t.read())
with open("score1.txt","r+",encoding="utf-8") as s:
lst1=s.readlines()
print(lst1)
lst2=[]
for i in lst1:
mark=i[-3:-1]
mark=eval(mark)
lst2.append(mark)
print(lst2)
average1=sum(lst2[0:5])/len(lst2[0:5])
average2=sum(lst2[6:9])/len(lst2[6:9])
print(average1)
print(average2)
lst4=lst2[2::3]
average3=sum(lst4)/len(lst4)
print(average3)
#os模块
import os
print(os.getcwd())#返回当前工作路径
#chdir(path):改变当前工作路径到指定的路径
print(os.listdir("C:\\Users\\Administrator\\Desktop\\python学习"))#listdir(path)返回指定路径下的文件和文件夹列表
#os.path.exists(path):路径存在则返回 True路径损坏返回 False
if os.path.exists('score.txt'):
print("file exists")
else:
print("file not exists")
os.popen("copy test1.txt test3.txt")#复制文件
new=open("D:hi.txt","a+")#new为自己定义的变量
#a+表示文件不存在就创建,存在继续追加
print("hello world",file=new)#使用file=的形式
new.close()
f=open("test1.txt","r",encoding="utf-8")
print(f.read())
#print(f.readlines())
f=open("score.txt","r",encoding="utf-8")
lst1=f.readlines()
print(lst1)
for date in lst1:
score=date.split()
print(score)
lst2=score[1:]
lst3=list(map(eval,lst2))#返回map对象在转换成列表
print(lst3)
sum1=sum(lst3)
print(sum1)
Loading…
Cancel
Save