百度飞桨零基础python速成营学习笔记
笔记目录 说明:笔记大部分内容包括图片等来自飞桨
说明:笔记大部分内容包括图片等来自飞桨)
1.python环境搭建及notebook使用
推荐使用 Anaconda大礼包来搭建python环境,Anaconda安装完成后,就可以直接开始学习python了
Anaconda下载页面
选择自己的版本
安装完成后打开jupter notebook,我们先用他编写python代码
同时,我们也可以注册AI Studio,AI Studio的项目文件也是notebook形式,其使用方法是一样的
notebook基本操作
notebook分单元格执行,方便显示程序中间结果和调试,适合初学者使用,后期也可以使用Pycharm编写python程序
AI Studio的Notebook支持多文件编辑, 支持.py, .json, .txt, .log等格式的在线编辑, 支持部分图片类型文件的在线预览.
Notebook快捷键
2.编程基础
2.1变量和运算符
标识符
需要自己起名字的都是标识符,包括变量,函数,类,模块等。标识符命名规则如下
1.由字母、下划线和数字组成
2.第一个字符不能是数字
3.不能和 Python 中的保留字(在编辑器里通常呈现出不同颜色)相同
此外,标识符中的字母是严格区分大小写的
变量
变量由三部分组成,标识,类型和值
标识:表示对象所存储的内存位置,由id(变量)来获取
类型:表示对象的数据类型,由type(变量)来获取
值:表示变量所存储的具体数据,print(变量),可以打印出变量的值
运算符
运算符主要关注其优先级,下图为运算符一栏表及其优先级
记不清楚就使用()来实现自己想要的顺序
2.2列表和字典
2.2.1列表
列表可以放入N个不同类型的数据,方便程序整体操作一组数据,列表相当于其他语言中的数组
列表存储的是n多个对象的引用(id),列表本身也有id,type,value
列表的创建及常用操作
#创建列表
list1 = ["a", "b", "c", "d","e","f"]
list2 = [1, 2, 3, 4, 5 ]
#list索引、切片
list2[2] #c
list1[2:5] #['c', 'd', 'e']
#list常用函数
list1.append('g') # 在末尾添加元素
list1.insert(2, 'ooo') # 在指定位置添加元素,如果指定的下标不存在,那么就是在末尾添加
list2 = ['z','y','x']
list1.extend(list2) #合并两个list list2中仍有元素
#count 计数 和 index查找
print(list1.count('a')) #3
print(list1.index('a')) #0
print('a' in list1) #True
#删除元素
list1 = ['a','b','a','d','a','f']
print(list1.pop(3)) #d
print(list1) #['a', 'b', 'a', 'a', 'f']
list1.remove('a') #['b', 'a', 'a', 'f']
print(list1)
#列表生成式
list_1 = [1,2,3,4,5]
[n+1 for n in list_1] #[3, 4, 5, 6, 7]
#生成器
#通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。
# 第一种方法:类似列表生成式
L = [x * x for x in range(10)]
g = (x * x for x in range(10))
next(g) #0,1,4,9,16,25
# 第二种方法:基于函数
def factor(max_num):
# 这是一个函数 用于输出所有小于max_num的质数
factor_list = []
n = 2
while n<max_num:
find = False
for f in factor_list:
# 先看看列表里面有没有能整除它的
if n % f == 0:
find = True
break
if not find:
factor_list.append(n)
yield n
n+=1
g = factor(10)
next(g)
2.2.1字典
字典dict
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度,这是以消耗大量内存为代价的
字典存储需经过hash工序,通过hash函数计算存储位置。键是不可变序列,可以使用str
#字典创建
word = {'apple':'苹果','banana':'香蕉'} #方式一:使用{}
dict(name = 'jack',age = 20)
#字典常用操作
#获取字典中的元素
word['apple'] #方式一:使用[],不存在则抛出KeyError
word.get('apple') #方式二:get()方法,不存在则返回None
#key的判断:in /not in
#字典元素的删除
del word['apple']
#字典元素的新增
word['orrange':'橙子']
#获取字典视图的三个方法 .keys() .values() .items()
#字典的特点:1.key不允许重复,value可以 2.字典中的key必须是不可变对象
#3.字典中的元素是无序的
#补充:字典排序
# 定义一个乱序的key值为0-5,value值为A-E的字典。
test_dict = {4: 'D', 2: 'B', 1: 'A', 5: 'E', 3: 'C'}
# 使用sorte()函数对“test_dict”进行排序,打印排序的结果。
print(sorted(test_dict))
print(dict(sorted(test_dict.items(), key = lambda x :x[1],reverse=True)))
[1, 2, 3, 4, 5]
{5: 'E', 4: 'D', 3: 'C', 2: 'B', 1: 'A'}
2.3元组和集合
2.3.1元组
另一种有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改
#元组的创建
tuple1 = (1, 2, 3, 4, 5 )
tuple2 = ('physics', 'chemistry', 1997, 2000) #方式一:直接使用()
t = tuple(('hello','python',99))
#注意事项:如果元组中是不可变对象,则不能再引用其他对象;如果元组中的对象是可变对象,则可变对象的引用不允许改变,但数据可以改变
可变对象:list dict set
不可变对象:tuple string int float bool
2.3.2集合
set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
#集合的创建
s = {'Python','hello',99} #方式一:直接使用{}
set([45,48,77]) #方式二:使用内置函数set()
#集合元素新增
s.add(80) #一次新增一个
s.uodate({40,50,60}) #一次至少新增一个
#集合元素的删除
s.remove(99) #一次删除一个指定元素,不存在则抛出异常
s.discard(99) #一次删除一个指定元素,不存在不抛异常
s.pop() #一次只删除一个任意元素
s.clear() #清空集合
2.4字符串
#字符串索引、切片
#切片的语法:[起始:结束:步长] 字符串[start: end: step] 这三个参数都有默认值,默认截取方向是从左往右的
start:默认值为0; end : 默认值未字符串结尾元素; step : 默认值为1;
如果切片步长是负值,截取方向则是从右往左的
name = 'molly'
name[1]
name[-4]
name[1:4]
name[::-1]
#字符串常用函数
my_string = 'hello_world'
my_string.count('o') #count()计数功能--o
my_string = 'hello_world'
my_string.find('o') #find()查找功能,返回从左第一个指定字符的索引,找不到返回-1 --4
my_string = 'hello_world'
my_string.index('o') #index() 查找返回从左第一个指定字符的索引,找不到报错
my_string = 'hello_world'
my_string.split('_') #split 字符串的拆分,按照指定的内容进行分割 --['hello', 'world']
my_string = 'hello_world'
my_string.replace('_',' ') #字符串的替换,从左到右替换指定的元素,可以指定替换的个数,默认全部替换 --'hello world'
my_string = ' hello world\n'
my_string.strip() #字符串标准化,默认去除两边的空格、换行符之类的,去除内容可以指定
my_string = 'hello_world'
my_string.upper()
my_string.lower()
my_string.capitalize() #字符串的变形:大写,小写,首字母大写
#字符串的格式化输出
accuracy = 80/123
print('老板!我的模型正确率是%.2f!' % accuracy) #--老板!我的模型正确率是0.65!
name = 'Molly'
hight = 170.4
score_math = 95
score_english = 89
print('大家好!我叫%s,我的身高是%d cm, 数学成绩%.2f分,英语成绩%d分' % (name, hight, score_math, score_english)) #--大家好!我叫Molly,我的身高是170 cm, 数学成绩95.00分,英语成绩89分
print('大家好!我叫{},我的身高是{:d} cm, 数学成绩{:.2f}分,英语成绩{}分'.format(name, int(hight), score_math, score_english)) #指定了 :s ,则只能传字符串值,如果传其他类型值不会自动转换,当你不指定类型时,你传任何类型都能成功,如无特殊必要,可以不用指定类型
name = 'Molly'
hight = 170.4
score_math = 95
score_english = 89
print(f"大家好!我叫{name},我的身高是{hight:.3f} cm, 数学成绩{score_math}分,英语成绩{score_english}分") # 一种可读性更好的方法 f-string
2.5流程控制
2.5.1分支结构
主要熟悉语法格式,python中使用缩进来控制循环体部分
# 60分以上的同学及格了
score = 80
if score < 60:
print('不及格')
else:
print('及格')
# 多分支:红灯停 绿灯行
light = '红灯'
if light == '红灯':
print('停')
elif light == '绿灯':
print('行')
else:
print('等一等')
2.5.2循环结构
# 从1数到9
number = 1
while number<10: # 注意边界条件
print(number)
number+=1
for i in range(9):
print(i+1)
for letter in 'Python': # 第二个实例
print( '当前字母 :', letter)
# break continue 提前终止循环,pass:等一会再写
# 查找list_1 中的数字
list_1 = [1,6,3,2,8,4]
for number in list_1:
if number == 3:
print('找到了!')
break
# continue : 跳过本轮
# 打印1-10中的偶数
for i in range(10):
num = i+1
if num%2 == 0:
print(num)
else:
continue
# pass: 占位,还没想好怎么写,先让程序跑起来
for i in range(5):
pass
3.函数
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
函数能提高应用的模块性,和代码的重复利用率。Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。
函数代码块以 def 关键词开头,后接函数标识符名称和圆括号()。
任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。
函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。
函数内容以冒号起始,并且缩进。
return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。
3.1函数创建和调用
使用def定义函数
def student_name(name):
"打印学生的名字"
print('姓名:', name)
return {'姓名':name}
rst = student_name('Alice')
rst # --姓名: Alice --{'姓名': 'Alice'}
# 返回多个值
def student_name_and_age():
"记录学生的名字和年龄"
name = input('请输入姓名\n')
age = int(input('请输入年龄\n'))
print(f'姓名:{name};年龄:{age}')
return name,age
rst = student_name_and_age()
type(rst) # --tuple
3.2参数
位置参数,缺省参数,可变参数,关键字参数,命名关键字参数
#位置参数是最简单的一种函数调用的方式。位置参数须以正确的顺序传入函数、数量必须和声明时的一样。
def student_name_and_age(name, age):
print('姓名:%s 年龄 %s' %(name, age))
student_name_and_age('张三', 18)
#调用函数时,缺省参数的值如果没有传入,则被认为是默认值。
def student_name_and_age(name, age='不愿透露'):
"设置默认参数"
print('姓名:%s 年龄 %s' %(name, age))
student_name_and_age('张三')
# 也可以为默认参数赋值
student_name_and_age('张三', 18)
#可变参数:顾名思义,可变参数就是传入的参数个数是可变的,可以是1个、2个到任意个,还可以是0个。
def all_student_names(*names):
for name in names:
print('姓名:', name)
all_student_names('张三','李四','王五')
#可变参数允许你传入0个或任意个参数,这些可变参数在函数调用时自动组装为一个tuple。
def all_student_names(names):
for name in names:
print('姓名:', name)
names = ('张三','李四','王五')
all_student_names(names)
#关键字参数:关键字参数允许你传入0个或任意个含参数名的参数,这些关键字参数在函数内部自动组装为一个dict。
def student_info(name, age, **kw):
print(f'我的名字叫:{name},年龄:{age},其它信息:{kw}')
student_info('张三', 18, height=180)
def student_info(name, age, **kw):
print(f'我的名字叫:{name},年龄:{age},其它信息:{kw}')
if 'city' in kw:
print('来自:', kw['city'])
student_info('张三', 18, height=180, city='北京')
def score_info(name, yuwen, shuxue):
print(name, '的语文成绩', yuwen)
print(name, '的数学成绩', shuxue)
def person_info(name, age, yuwen, shuxue):
print('姓名:', name, '年龄',age)
score_info(name, yuwen, shuxue)
person_info('张三', 18, 65, 60)
def score_info(name, **kw):
if '语文成绩' in kw:
print(name, '的语文成绩', kw['语文成绩'])
if '数学成绩' in kw:
print(name, '的数学成绩', kw['数学成绩'])
def person_info(name, age, **kw):
print('姓名:', name, '年龄',age)
score_info(name, **kw)
score_cfg = {'语文成绩':65, '数学成绩':60}
person_info('张三', 18, **score_cfg)
#命名关键字参数:如果要限制关键字参数的名字,就可以用命名关键字参数
def print_person_info(name, age, *, height, weight):
print('我的名字叫:', name, '年龄:', age,'身高', height, '体重', weight)
print_person_info('张三', 18, height=180, weight=75)
#参数的组合:在Python中定义函数,可以用必选参数、默认参数、可变参数、关键字参数和命名关键字参数,
这5种参数都可以组合使用。但是请注意,参数定义的顺序必须是:必选参数、默认参数、可变参数、命名关键字参数和关键字参数。
3.3作用域与返回值
变量的作用域和global变量
1.局部变量 作用域:在函数内
2.全局变量 作用域:在函数外
函数优先使用局部变量 在没有局部变量的情况下, 使用全局变量,使用global在函数内部声明全局变量
定义在函数内部的变量,函数结束之后自动消亡
3.4lambda匿名函数
python 使用 lambda 来创建匿名函数。
lambda 只是一个表达式,函数体比 def 简单很多。
lambda 的主体是一个表达式,而不是一个代码块。仅仅能在 lambda 表达式中封装有限的逻辑进去。
lambda 函数拥有自己的命名空间,且不能访问自有参数列表之外或全局命名空间里的参数。
虽然 lambda 函数看起来只能写一行,却不等同于 C 或 C++ 的内联函数,后者的目的是调用小函数时不占用栈内存从而增加运行效率。
# lambda 若干个输入参数 : 返回值的表达式
lambda arg1, arg2: arg1 + arg2
(lambda arg1, arg2: arg1 + arg2 )(1, 2) #--3
# 加法运算 接受两个参数,返回参数之和
add = lambda arg1, arg2: arg1 + arg2
add(1,2)
4.面向对象
4.1类与对象
类是多个类似事物组成的群体的统称,能够帮助我们快速理解和判断事物的性质
数据类型
不同数据类型属于不同的类,使用内置函数type可以查看数据类型
对象
1,20,50都是int类之下包含的相似的不同个例,这个个例的专业术语成为实例或对象
python中一切皆对象
4.2类的创建
class Athlete:
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
def top3(self):
return sorted(set([sanitize(t) for t in self.times]))[0:3]
def sanitize(self,time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins,secs) = time_string.split(splitter)
return (mins+'.'+secs)
如何定义类
class Athlete:
第一部分:class定义类的关键字,Athlete符合python标识符命名规则,:表示类内容的开始
def init(self,a_name,a_dob=None,a_times=[]):
第二部分:def定义函数的关键字,init 方法是一个特殊方法会在实例化对象时自动调用,我们会在这个方法中对数据进行赋值。self作为类中函数的第一个参数,方便该方法调用该类的其他属性和方法。
第三部分:自定义的属性和方法
代码通常称为类的方法,数据通常称为类的属性,实例化的对象称为实例
使用类的好处
1.降低复杂性—>更少的bug—>提高可维护行
2.类可以将数据与函数绑定在一起,使代码模块化
3.调用数据和函数,使用对象名.的方式,使代码更加优雅
如何使用类
1.创建对象
对象名 = 类名(参数)
2.使用.调用类的方法和属性
对象.属性名
对象.方法名()
类属性
所有对象共享的数据
类方法
所有对象共享的方法
class Athlete:
#运动员集训了,要买东西的同学要把地址改一下
address = '中国足球协会训练基地xx街xx号'
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
def top3(self):
return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
def sanitize(self,time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins,secs) = time_string.split(splitter)
return (mins+'.'+secs)
@classmethod
def changeAddress(self):
self.address = '中国田径训练基地xx街xx号'
如何使用:
定义:
方法定义时,使用@classmethod标记
调用:
类名.类方法
对象.类方法
私用的属性和方法的定义:
在属性和方法名前加 __ 两个下划线
4.2封装、继承与方法重写
面向对象的三大特征
封装:提高程序的安全性,将属性和方法包装到类对象中,在方法内部对属性进行操作,在
类对象的外部调用方法。这样,无需关心方法内部的具体实现细节,从而隔离了复杂度
继承:提高代码的复用性
多态:提高程序的可扩展性和可维护性
#定义橄榄球运送员类
class Rugby(Athlete):
def __init__(self,a_name,a_bod,a_squat,a_times):
#调用父类__init__
Athlete.__init__(self,a_name,a_bod,a_times)
#深蹲次数
self.squat = a_squat
# 继承后下面两个函数就在Rugby类中,只是看不到而已
# def top3(self):
# return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
# def sanitize(self,time_string):
# if '-' in time_string:
# splitter = '-'
# elif ':' in time_string:
# splitter = ':'
# else:
# return (time_string)
# (mins,secs) = time_string.split(splitter)
# return (mins+'.'+secs)
loren = get_coach_data('mywork/loren.txt')
rugby = Rugby(loren.pop(0),loren.pop(0),loren.pop(0),loren)
print('姓名:%s,生日:%s,深蹲:%s个,最块的3次成绩:%s' %(rugby.name,rugby.dob,rugby.squat,rugby.top3()))
#姓名:2011-11-3,生日:270,深蹲:3.59个,最块的3次成绩:['3.11', '3.23', '4.10']
如何使用
定义:
class 子类名(父类名):
情况1,如果子类有新增的属性,那么需要在子类__init方法中,调用父类的__init__
情况2,如果子类没有新增的属性,子类不需要写__init__方法
使用:
对象名 = 子类名(参数)
继承的好处:代码重用,升级功能(重写),新增功能(新的方法)
class Rugby(Athlete):
def __init__(self,a_name,a_bod,a_squat,a_times):
Athlete.__init__(self,a_name,a_bod,a_times)
self.squat = a_squat
def top3(self):
return sorted([self.sanitize(t) for t in self.times])[-3:]
loren = get_coach_data('mywork/loren.txt')
rugby = Rugby(loren.pop(0),loren.pop(0),loren.pop(0),loren)
print('姓名:%s,生日:%s,深蹲:%s个,最慢的3次成绩:%s' %(rugby.name,rugby.dob,rugby.squat,rugby.top3()))
方法重写
子类方法与父类方法完全相同,子类若重写了父类的方法,则子类对象调用方法时就是调用的自己类中重新的方法。
如果有其他的教练,只需要top3中的,3个最短记录呢(可以重复)?有更多的教练对top3的更多要求呢?
class OtherAthlete(Athlete):
def __init__(self,a_name,a_bod,a_squat,a_times):
Athlete.__init__(self,a_name,a_bod,a_times)
self.squat = a_squat
def top3(self):
return sorted([self.sanitize(t) for t in self.times])[0:3]
mark = get_coach_data('mywork/mark.txt')
mark = OtherAthlete(mark.pop(0),mark.pop(0),mark.pop(0),mark)
print('姓名:%s,生日:%s,深蹲:%s个,最快的3次成绩:%s' %(mark.name,mark.dob,mark.squat,mark.top3()))
#姓名:mark,生日:2010-2-4,深蹲:300个,最快的3次成绩:['3.11', '3.11', '3.23']
多态性:一个事物多种形态
上面例子中print_rugby的参数athlete,athlete.name,athlete.top3()的行为由athlete的子类决定。
多态的好处是:减少重复代码,分离经常改变的代码与不经常改变的代码,使得代码可维护性提高。
def obj_factory(name,filename):
with open(filename) as f:
line = f.readline()
templ = line.strip().split(',')
if name == 'r':
return Rugby(templ.pop(0),templ.pop(0),templ.pop(0),templ)
elif name == 'oa':
return OtherAthlete(templ.pop(0),templ.pop(0),templ.pop(0),templ)
oa = obj_factory('oa','mywork/mark.txt')
print(oa.name)
多继承
class Father():
def talk(self):
print("---爸爸的表达能力---")
class Mather():
def smart(self):
print("---妈妈聪明的头脑---")
class Child(Father,Mather):
pass
child1 = Child()
child1.talk()
child1.smart()
问题:在每个需要Athlete的时候,我都需要把Athlete类的定义复制粘贴要用的代码上方。
import sys
sys.path.append('mywork')
# import athlete
# print(dir(athlete))
from athlete import *
loren = get_coach_data('mywork/loren.txt')
mark = get_coach_data('mywork/mark.txt')
loren = Rugby(loren.pop(0),loren.pop(0),loren.pop(0),loren)
mark = OtherAthlete(mark.pop(0),mark.pop(0),mark.pop(0),mark)
def print_rugby(athlete):
print(athlete.name)
print(athlete.dob)
print(athlete.squat)
print(athlete.top3())
print_rugby(loren)
print_rugby(mark)
import sys
导入sys模块 sys.path.append(‘work’)
将模块athlete.py添加到模块搜索路径
from athlete import *
导入athlete模块,使用athlete模块下的所有代码
问题:如果有一个模块mymodule.py中也包含get_coach_data函数,该怎么区分这两个函数呢?
import sys
sys.path.append('work')
from p1.mymodule import *
from p2.mymodule import *
import p1
import p2
p1.mymodule.demo()
p2.mymodule.demo()
5.文件操作
5.1文件处理摸模型
输入,处理,输出。
输入:读取4个队员的训练数据,读取4个文件
james.txt 2-34,3:21,2,34,2.45,3.01,2:01,2:01,3:10,2-22
sarah.txt 2:58,2.58,2:39,2-25,2:55,2:54,2.18,2:55,2:55
julie.txt 2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
mikey.txt 2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38
处理:标准化数据,切分数据,top3(最快的3个时间)
输出:将每个人的信息打印在屏幕上显示
kelly教练,每次训练结束后,还要同步更新4个文件太麻烦了,需要把所有记录写在一个文件中
f = open('work/train_data_cor.txt')
line = f.readline()
print(line)
line = f.readline()
print(line)
f.close()
open() 为bif(内置函数),参数有多个,必须的是文件路径。 返回的一个文件对象。
file.readline(),读取文件中的一行。
import sys
#读取整个文件内容
f = open('work/train_data_cor.txt')
line = f.readline()
while(line != ''):
print(line)
line = f.readline()
f.close()
#更好的方式
f = open('work/train_data_cor.txt')
for line in f:
print(line)
f.close()
数据异常情况:1.使用异常跳过有问题的数据 2.增加代码判断
#使用异常
f = open('work/train_data_wrg.txt')
for line in f:
data = line.strip().split(',')
try:
print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))
except:
pass
f.close()
#代码判断
f = open('work/train_data_wrg.txt')#1
for line in f:#2
data = line.strip().split(',')
if len(data) != 1:
print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))
f.close()#3
#clean的写法,三行变一行
with open('work/train_data_cor.txt') as f:
for line in f:
data = line.strip().split(',')
print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))
5.2file对象的函数列表
with open('work/train_data.txt') as f:
data = f.read()
print('整个文件\n'+data)
f.seek(0)
data = f.read(10)
print('读取指定大小的文件内容\n'+data)
print(f.tell())
写入文件内容
f = open('work/data.txt','w')
f.write('this is file content')
f.close()
open(‘work/data.txt’,‘w’)第一个参数文件路径,第二个参数打开文件的模式
f.write(‘this is file content’)参数为写入的内容 f.close()关闭文件
5.3对象转JSON
import json
class Athlete(json.JSONEncoder):
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
def top3(self):
return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
def sanitize(self,time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins,secs) = time_string.split(splitter)
return (mins+'.'+secs)
with open('work/train_data_cor.txt') as f:
data = f.readline().strip().split(',')
ath = Athlete(data.pop(0),data.pop(0),data)
print(ath)
ath_json = json.dumps(ath.__dict__)
内中的json形式的变量保存到文件
with open('work/json.txt','w') as f:
json.dump(ath_json,f)
with open('work/json.txt') as f: #读取json文件内容
ath = json.load(f)
print(ath)
目录访问
import os
#返回当前工作目录
current_path = os.getcwd()
print('当前路径:'+current_path)
#改变当前工作目录
os.chdir('/home/aistudio/work')
#运行mkdir命令
os.system('mkdir today')
from pathlib import Path
#返回当前绝对路径
abs_path = os.path.abspath('')
print('abs_path:'+abs_path)
#路径是否存在
Path(abs_path).exists()
print('当前路径:'+os.getcwd())
listdir = os.listdir()
#返回当前路径下文件和文件夹名
print(listdir)
#是否为文件夹
os.path.isdir('/home/aistudio/work/today')
显示work路径下的所有类型为txt的文件
import os
listdir = os.listdir('/home/aistudio/work')
target = []
for name in listdir:
#防止文件名与文件夹名一样的情况
# print(os.path.isfile(name))
temp = name.split('.')
(filename,filetype) = (temp.pop(0),temp.pop(0))
if filetype == 'txt':
target.append(name)
# print('name:%s,type:%s' %(filename,filetype))
print(target)
temp = name.split(’.’)在name为dir1时出现问题
import os
path = '/home/aistudio/work'
listdir = os.listdir(path)
target = []
for name in listdir:
#防止文件名与文件夹名一样的情况
# print(os.path.isfile(name))
if os.path.isfile(path+'/'+name):
temp = name.split('.')
(filename,filetype) = (temp.pop(0),temp.pop(0))
if filetype == 'txt':
target.append(name)
# print('name:%s,type:%s' %(filename,filetype))
#如果dir1中包含文件
import os
target = []
path = '/home/aistudio/work'
listdir = os.listdir(path)
for name in listdir:
#防止文件名与文件夹名一样的情况
if os.path.isfile(path+'/'+name):
temp = name.split('.')
(filename,filetype) = (temp.pop(0),temp.pop(0))
if filetype == 'txt':
target.append(name)
else:
#如果是文件夹,需要读取该文件夹的列表
dir_path = path+'/'+name
listdir = os.listdir(dir_path)
for name in listdir:
#防止文件名与文件夹名一样的情况
if os.path.isfile(dir_path+'/'+name):
temp = name.split('.')
(filename,filetype) = (temp.pop(0),temp.pop(0))
if filetype == 'txt':
target.append(name)
print('结果:'+str(target))
如果dir1中又包含文件夹
import os
def recur(path):
listdir = os.listdir(path)
for name in listdir:
if name[0] is '.' or name[0] is '_':
continue
next_path = path+'/'+name
if os.path.isfile(next_path) :
# print(next_path + '=====isfile')
temp = name.split('.')
(filename,filetype) = (temp.pop(0),temp.pop(0))
if filetype == 'txt':
target.append(name)
else:
recur(next_path)
return os.path.dirname(next_path)
path = '/home/aistudio/work'
target = []
recur(path)
print(target)
#制造数据
with open('work/loren.txt','w+') as f:
for i in range(5000000):
f.write('loren,2011-11-3,270,3.59,4.11,3:11,3:23,4-10,3-23,4:10,4.21,4-21')
f.write('\n')
#只使用进程的方式
print('压缩作业开始了,请您耐心等待...')
infile = 'work/loren.txt'
outfile = 'work/myarchive.zip'
f = zipfile.ZipFile(outfile, 'w', zipfile.ZIP_DEFLATED)
f.write(infile)
f.close()
print('压缩作业结束了,请问还需要帮您做什么呢?')
import threading, zipfile
class AsyncZip(threading.Thread):
def __init__(self, infile, outfile):
threading.Thread.__init__(self)
self.infile = infile
self.outfile = outfile
def run(self):
f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
f.write(self.infile)
f.close()
print('压缩完成,您要的文件在:', self.outfile)
background = AsyncZip('work/loren.txt', 'work/myarchive.zip')
print('压缩作业开始了,请您耐心等待...')
background.start()
print('我正在为您压缩,请问还需要帮您做什么呢?')
background.join()
补充说明:文章性质为个人笔记,大部分内容来源于飞桨,仅用于学习交流,这是我第一次写笔记
如有错误和不足希望大家谅解