python部分内容总结

python部分内容总结(持续更新)

import json
class Person:
    def __init__(self,name='',age=1):
        self.name=name;
        self.age=age;
    def getName(self):
        return self.name
#面向对象编程,生成这个新的类时调用初始化函数。对于这个类的函数,里面一定要写self
class student(Person):
    def __init__(self,grade=1,school=''):
        super.__init__()
        self.grade=grade
        self.school=school
#如何继承父类
n=input("")
#读入,读进来的默认是字符串,括号里的可以先输出要输出的
with open("","w") as f:
    f.write("")
with open('',"a") as f:
    f.write('')
#w是打开文件并且删除原来的然后写入要写的内容,a是直接往后写。这种with方法不需要close。
#如果直接定义变量然后读写文件需要close
#文件没有则直接创建
with open('','r') as f:
    content=f.read()
    #全部读取
    content=f.readlines()
    #按照每行读取,并且将读取到的存入一个list
    content=f.readline()
    #这是只读取一行
#如果是r+的话则是先打开一个文件,如果能打开则写入
with open("") as f:#打开一个文件
    for line in f:#遍历这个文件全部的行
        line.rstrip()#将每一行最后的回车去掉
try:
    f=open("")
except Exception as e:
    print(e)
else:
    a=1
#捕获异常
a_dic={
    
    'name':'1',2:2,3:3}
with open('','w') as f:
    json.dump(a_dic,f)
#把a_dic(字典)存储到文件f里
with open('') as f:
    content=json.load(f)
#从f里面读取json格式数据,生成一个字典



猜你喜欢

转载自blog.csdn.net/weixin_42975688/article/details/115405964