大纲
Python文件类型及汇总
一、元组
1 特征
1.任意对象的有序集合
2.通过下标访问
3.不可变
4.长度固定,任意类型,任意嵌套
>>> t = (1,2,3,4,5) >>> t[0] = 2 Traceback (most recent call last): File \"<stdin>\", line 1, in <module> TypeError: \'tuple\' object does not support item assignment
2 声明
(value1,value2,…)
3 操作
1.index(val):查找索引
2.count(val):统计数据
>>> t (1, 2, 3, 4, 5) >>> t.index(3) 2 >>> t.count(3) 1
元组代码
(1,2) #定义一个元组 (1, 2) (1,2)+(3,4)#增加元组 (1, 2, 3, 4) t=[1,2,3,4,5] res=[x**2 for x in t] #计算出t中元素的平方并放在res中 res [1, 4, 9, 16, 25] t.index(3) #检索3的位置 2 t.count(3) #数元组t中3的个数 1 from collections import namedtuple #引入namedtuple给员工赋值 employee=namedtuple(\"employee\",[\"named\",\"age\",\"department\",\"salary\"]) #定义一个员工模板 Jerry=employee(\"Jerry\",30,\"财务部\",\"9000.00\")#给名叫Jerry的员工赋值 Jerry employee(named=\'Jerry\', age=30, department=\'财务部\', salary=\'9000.00\') Jerry.age #读取Jerry的年龄 30
注意事项:列表 元组的转换
元组解析
元组内部列表的修改:
二、文件
1 基本语法
file = open(‘文件名\’,mode)
三种模式
mode:r ,w ,a
>>> myfile = open(\'hello.txt\',\'w\') #若没有,自动创建文件
2 操作
read、readlines、close方法
>>> myfile = open(\'hello.txt\',\'w\') >>> myfile.write(\"你好啊,我叫赛利亚\\n\") #写操作 10 >>> myfile.close()
>>> f = open(\'hello.txt\') >>> f.read() \'你好啊,我叫赛利亚\\n\' >>> f.read() \'\' >>> f = open(\'hello.txt\') >>> f.readline() #readline一次读取一行,返回字符串 \'你好啊,我叫赛利亚\\n\' >>> f.readline() \'\' >>> l = open(\'hello.txt\').readlines() #readline一次读取全部行,返回列表 >>> l [\'你好啊,我叫赛利亚\\n\']
with open() as …用于临时打开文件,结束后自动close释放资源(推荐这种用这种方式打开文件进行操作)
>>> f = open(\'hello.txt\') >>> f.read() \'你好啊,我叫赛利亚\\n\' >>> f.read() \'\' >>> f = open(\'hello.txt\') >>> f.readline() #readline一次读取一行,返回字符串 \'你好啊,我叫赛利亚\\n\' >>> f.readline() \'\' >>> l = open(\'hello.txt\').readlines() #readline一次读取全部行,返回列表 >>> l [\'你好啊,我叫赛利亚\\n\']
网
易
云
课
堂
文件权限
rb 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。一般用于 非文本文件如图片等。
注意:二进制文件把内容表示为一个特殊的 bytes 字符串类型。
# file = open(\"demo1/1.txt\",\"rb\") file = open(\"demo1/1.png\",\"rb\") ret = file.read() #b\'huangzhi\' huangzhi print(ret) file.close()
r+ 打开一个文件用于读写。文件指针将会放在文件的开头。
file = open(\"demo1/1.txt\",\"r+\") # ret = file.read() #读取全部内容 # print(ret) file.write(\"guyin\") #从头写入,原有内容会逐渐被覆盖 file.close()
rb+ 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。一般用于 非文本文件如图片等。
wb 以二进制格式打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。一般用于非文本文件如图片等。
from demo1.img import img2 file = open(\"demo1/2.jpg\",\"wb\") file.write(img2) file.close()
w+ 打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即 原有内容会被删除。如果该文件不存在,创建新文件。
file = open(\"demo1/1.txt\",\"w+\") file.write(\"hello world\") ret = file.read() print(ret) file.close()
a 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。
也 就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件 进行写入。
#在demo1下的111.txt中追加“guyin” # file = open(\"demo1/111.txt\",\"a\") file = open(\"demo1/3.txt\",\"a\") file.write(\"guyin\") file.close()
ab 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文 件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在, 创建新文件进行写入。
a+ 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。
文 件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
file = open(\"demo1/111.txt\",\"a+\") file.write(\"yangyong\") ret = file.read() print(ret) file.close()
ab+ 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文 件的结尾。如果该文件不存在,创建新文件用于读写。
三、pickle存储和读取python对象
dump(对象,目标文件)
load(文件)
f = open(\'datafile.pkl\',\'wb\') >>> import pickle >>> d = {\'a\':1,\'b\':2} >>> pickle.dump(d,f) >>> f.close() >>> f = open(\'datafile.pkl\',\'rb\') >>> data = pickle.load(f) >>> data {\'a\': 1, \'b\': 2}
暂无评论内容