(一) 今日进展及明日计划
今日进展:
1.到今天为止总算把python的全部语法过完了.
存在的问题:
1.为防止遗忘,接下来几天得想办法把学到的python语法用起来.
明日计划:
1.可以放心去修改python上位机的bug了;
2.读英文文章,争取这周把摘要和引言部分先写完.
(二) 具体进展情况
(14) 文件操作
这里是引用:https://zhidao.baidu.com/question/176982986770397124.html
这里是引用:https://blog.youkuaiyun.com/qq_26442553/article/details/81626442
# 高级:pandas、numpy
#!/usr/bin/python
# -*- coding:utf-8 -*-
txt =open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt','r')
print(txt.read())
> 有bug,根本行不通
txt = open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt')
# txt_read = txt.read()
# print(txt_read)
# 按行读
lines = txt.readlines() # read()和readlines()不能同时出现
print(type(lines))
print(lines)
print('----------------------------------------------------')
for line in lines:
print('cur_line',line)
# 关闭文件
# txt.close()
txt = open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt','w') # 写文件(覆盖原内容)
txt.write('利威尔.阿克曼\n')
txt.write('莎夏.布劳斯\n')
txt.write('希斯特利亚.雷斯')
txt.close()
txt = open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt','a') # 追加内容
txt.write('\n进击的巨人\n')
txt.write('始祖巨人\n')
txt.write('坐标の力')
txt.close()
txt = open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt','w')
for i in range(10):
# txt.write(str(i)+'\n')
txt.write(str(i)+'\n')
txt2 = open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt','r') #这里并没有打印出来
print(txt2.read())
txt = open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt','w')
for i in range(10):
# txt.write(str(i)+'\n')
txt.write(str(i)+'\n')
txt.close() # 加上这句就能打印出来了
txt2 = open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt','r')
print(txt2.read())
txt = open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt','w')
try:
for i in range(11):
10/(i-10)
txt.write(str(i)+'\n')
except Exception:
print('error',i)
finally:
txt.close()
# with可以自动完成try、except、finally,遇到错误也会自动关闭文件,不用加close
with open('C:/Users/张志远/Desktop/python_Qt/A/abc.txt','w') as f: # f是别名
f.write('莱纳,你坐啊')
(15) 类的基本定义
class people:
'帮助信息:XXXXX'
number = 100
def __init__(self,name,age): # 初始化方法
self.name = name
self.age = age
def display(self):
print('number =',people.number) # number是在people这个类下的,只写number可不行
def display_name(self):
print(self.name)
p1 = people('zzy',30)
p2 = people('luffy','40')
print('------------从类中打印属性---------------')
print(people.__doc__)
print(p1.name) # 打印属性不用加()
print(p2.name)
print(p1.display())
print(p2.display())
# 改名字
print('----------------改名字-------------------')
print(p1.name)
p1.name = '艾伦.耶格尔'
print(p1.name)
# 删除属性
print('---------------删除属性-----------------')
# del p2.name
print(p2.name)
# 看下有没有某个属性
print('------------看某属性是否存在--------------')
print(hasattr(p1,'name'))
print(hasattr(p1,'sex'))
# 先has下看有没有某个属性,有的话就get出来
print('--------------get出某个属性--------------')
print(getattr(p1,'name'))
# set改名
print('---------------set改名------------------')
print(p1.name)
setattr(p1,'name','尤弥尔')
print(getattr(p1,'name'))
# 删除属性
print('-------------del删除属性-----------------')
# delattr(p1,'name')
print(getattr(p1,'name'))
# 打印类中的一些信息
print('----------打印类中的一些信息--------------')
print(people.__doc__) # 帮助文档
print(people.__name__) # 类的名字
print(people.__module__) # 类的定义
print(people.__bases__) # 类的副类
print(people.__dict__) # 类的结构
(16) 类的属性操作
# # 子类继承父类属性,就不必重复定义了,如狗是动物,就应该具备动物的共有属性
class parent: # 定义父类
number = 100
def __init__(self):
print('调用父类构造函数')
def parentM(self):
print('调用父类方法')
# def setattr(self.attr): # 应当是‘,’,而我却看成‘.’了
# parent.parentattr = attr
def setattr(self,attr):
parent.parentattr = attr
def getattr(self):
print('父类属性:',parent.parentattr)
class child(parent): # 定义子类
def __init__(self):
print('调用子类构造函数')
def childM(self):
print('调用子类方法')
# 子类实例化,调用子类
c = child()
c.childM()
c.parentM()
c.setattr(100)
c.getattr()
# # 子类继承父类属性,就不必重复定义了,如狗是动物,就应该具备动物的共有属性
# 父类的方法对子类不适用,就要进行方法的重写
class parent: # 定义父类
number = 100
def __init__(self):
print('调用父类构造函数')
def parentM(self):
print('调用父类方法')
# def setattr(self.attr): # 应当是‘,’,而我却看成‘.’了
# parent.parentattr = attr
def setattr(self,attr):
parent.parentattr = attr
def getattr(self):
print('父类属性:',parent.parentattr)
def newM(self): # 父类的方法不满足于子类,进行方法的重写,此方法在子类中要重写一遍
print('父类要按重新的方法')
class child(parent): # 定义子类
def __init__(self):
print('调用子类构造函数')
def childM(self):
print('调用子类方法')
def newM(self): # 和父类中那个是一样的
print('子类给他改掉了')
# 子类实例化,调用子类
c = child()
c.childM()
c.parentM()
c.setattr(100)
c.getattr()
c.newM() # 打印的是子类的方法
(17) 时间操作
import time
print(time.time()) # 从1970开始经历了多少时间
print(time.localtime(time.time()))
print(time.asctime(time.localtime(time.time())))
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
print(time.strftime('%y-%m-%d %H:%M:%S',time.localtime()))
print('----------------------------------------------')
import calendar
print(calendar.month(2019,8))
# help(time)