record-08 面向对象编程 文件读取和文件写入

#__author: hasee
#date:  2018/1/15
from random import choice


# 分析参与程序执行期间的对象-队伍中的队员

# 分析对象的行为属性、数据属性
# 队员的行为属性-射门、守门
# 队员的数据属性-得分

# 定义类,描述同类对象
class User:
    score = 0

    def u_shemen(self):
        print("请选择射门方向:")
        u = input()
        return u

    def u_shoumen(self):
        print("请选择守门方向:")
        u = input()
        return u


class Com:
    score = 0

    def c_shemen(self):
        d = ['上', '下', '左', '右']
        c = choice(d)
        return c

    def c_shoumen(self):
        d = ['上', '下', '左', '右']
        c = choice(d)
        return c


# 梳理程序执行过程,并分析执行期间对象如何参与
for i in range(1, 6):
    print("当前是第%d轮比赛" % i)
    # 1、找到用户队伍中的一名队员,让他完成一次射门
    u1 = User()
    ud1 = u1.u_shemen()
    # 2、找到程序队伍中的一名队员,让他完成一次守门
    c1 = Com()
    cd1 = c1.c_shoumen()
    # 3、判断球进没进
    if ud1 == cd1:
        print("球没进")
    else:
        print("球进了")
        User.score = User.score + 1
    # 4、让两只队伍的队员互换,用户队员守门,程序队员射门
    ud2 = u1.u_shoumen()
    cd2 = c1.c_shemen()
    if ud2 == cd2:
        print("球没进")
    else:
        print("球进了")
        Com.score = Com.score + 1
    print("本轮比赛比分%d:%d" % (User.score, Com.score))


from random import choice
#分析程序执行期间的对象-裁判

#分析对象的行为属性、数据属性
#行为属性-安排用户射门、程序守门、并判断是否射进
#       -安排程序射门、用户守门、并判断是否射进
#数据属性-两只队伍的比分

#定义类
class CP:
    def __init__(self):
        self.u_score=0
        self.c_score=0
    def u_c(self):
        print("请用户选择射门方向:")
        u=input()
        d=['上','下','左','右']
        c=choice(d)
        if u==c:
            print("用户射门未进")
        else:
            print("用户射门进了")
            self.u_score=self.u_score+1
    def c_u(self):
        print("请用户选择守门方向:")
        u=input()
        d=['上','下','左','右']
        c=choice(d)
        if u==c:
            print("程序射门未进")
        else:
            print("程序射门进了")
            self.c_score=self.c_score+1


#梳理程序执行的过程,并明确过程中对象如何参与
cp1=CP()
for i in range(1,6):
    print("当前是第%d轮比赛"%i)
    cp1.u_c()
    cp1.c_u()
    print("本轮比赛比分%d:%d"%(cp1.u_score,cp1.c_score))


from random import choice

class User:
    def u_shemen(self):
        print("请选择射门方向:")
        u=input()
        return u
    def u_shoumen(self):
        print("请选择守门方向:")
        u=input()
        return u

class Com:
    def c_shemen(self):
        d=['上','下','左','右']
        c=choice(d)
        return c
    def c_shoumen(self):
        d=['上','下','左','右']
        c=choice(d)
        return c

class CP:
    def __init__(self):
        self.u_score=0
        self.c_score=0
    def u_c(self):
        u1=User()
        u=u1.u_shemen()
        c1=Com()
        c=c1.c_shoumen()
        if u==c:
            print("用户射门未进")
        else:
            print("用户射门进了")
            self.u_score=self.u_score+1
    def c_u(self):
        u1=User()
        u=u1.u_shoumen()
        c1=Com()
        c=c1.c_shemen()
        if u==c:
            print("程序射门未进")
        else:
            print("程序射门进了")
            self.c_score=self.c_score+1


cp1=CP()
for i in range(1,6):
    print("当前是第%d轮比赛"%i)
    cp1.u_c()
    cp1.c_u()
    print("本轮比赛比分%d:%d"%(cp1.u_score,cp1.c_score))


#面向对象编程三大特性
#封装、继承、多态
#封装-减少后续代码维护成本
#继承-增强代码重用性,提高代码设计效率
#多态-通过对继承的属性进行重新定义(重写),可以让其在被调用时具有不一样的表现
class Father:
    def drink(self):
        print("DRINKING 1KG")

class Mother:
    def drink(self):
        print("DRINKING 0.5KG")

class Son(Father,Mother):    #括号表示继承
    def smoke(self):
        print("SMOKING")


#多重继承中,如果父类中有同名方法,一定要弄清继承顺序
#在变量__mro__存放
#深度优先、广度优先
class A:
    def print_x(self):
        print('A')
class B:
    def print_x(self):
        print('B')
class C(A,B):
    pass

class D:
    def print_x(self):
        print('D')

class E(C,D):
    pass


print(E.__mro__)
e1=E()
e1.print_x()


#文件读取

#打开文件,open函数
#需要明确文件的路径(转义字符)和名称(后缀名)
#文件路径:相对路径、绝对路径
#open函数执行后,返回一个文件对象

f=open("abc.txt")   #把文件存在缓存中


#读取内容,把文件从缓存中,读取到程序中
#data=f.read() #read()一次将文件全部内容读取出来,并保存在一个字符串中
#print(data.split('
'))

#data1=f.readline() #readline()每一次读取文件中的一行数据,并保存在一个字符串中
#print(data1)
#data2=f.readline()
#print(data2)

#读取文件中每一行记录,并分别统计每一行记录的字符数量
data=f.readlines() #readlines()默认读取文件中的全部内容,保存在一个列表中,列表中的每一个元素(字符串类型)都对应文件中的一行记录
print(data)
print(len(data))
'''
for i in data:
    print(len(i))

'''
#关闭文件
f.close()


#统计记事本文件中有几行记录


#文件写入

#打开文件,open()第二个参数代表打开模式
#w覆盖写入 a追加写入 r只读模式 x新建文件
#t字符串模式  b字节模式
#+ 即读又写模式

#f.seek(-5,1) #第一个参数表示偏移量  第二个参数表示移动开始位置(0文件开头,1当前位置,2文件结尾)


f=open('abc.txt','w')
#写入内容
f.write('hello world') #一次写入一条数据,参数为字符串类型  #从程序中写入文档

#l1=['hello
','world
','Bye']
#f.writelines(l1) #一次写入多行数据,参数为列表类型,列表中的每一个元素都是一个字符串,表示要写入的一条数据


#关闭文件
f.close()

#统计文件中每名学生的总成绩,并写回文件
张三 70 80 90
李四 80 70 60
王五 90 80 90

f=open('student.txt')
user_info=f.readlines()
print(user_info)

user_new=[]
for u in user_info:
    print('****************************')
    u_l=u.split()
    print(u_l)
    result=int(u_l[1])+int(u_l[2])+int(u_l[3])
    u_l.append(str(result))
    print(u_l)
    u1=' '.join(u_l)+'
'
    user_new.append(u1)
    print('****************************')
f.close()

print(user_new)
f=open('student.txt','w')
f.writelines(user_new)
f.close()

  

原文地址:https://www.cnblogs.com/minkillmax/p/8298789.html