中心主题 ╰+哭是因爲堅強的太久メ 2023-08-17 16:52 110阅读 0赞 ## 5.数据类型 ## ### 元组(tuple) ### * 特点 * 任意对象的有序集合 * 通过下标来访问 * 属于‘’不可变类型‘’ * 长度固定,任意长度,任意类型 * 声明 * (元素,) * 索引查询:index(val) * 统计数量:count(val) * namedtuple * > > > from collections import namedtuple > > > Employee=namedtuple(‘Employee’,\[‘name’,‘age’,‘department’,‘salary’\]) > > > > > > jerry=Employee(‘jerry’,age=30,department=‘财务部’,salary=9000.00) > > > jerry > > > Employee(name=‘jerry’, age=30, department=‘财务部’, salary=9000.0) > > > > jerry.salary > > > 9000.0 ### 文件 ### * 基本语法 * file=open(‘文件名’,mode) * mode * r(read) * w(write) * a(append) * b(binary) * 文件操作 * read() * readline() * readlines() * close() * 实战1 * 1.打开pycharm的Python Console * 2.逐行输入:myfile=open(‘hello.txt’,‘w’) myfile.write(‘武汉大学\\n’) 5 myfile.write(‘xiaodai/n’) 9 myfile.close() myfile.read() * 3.刷新目录:synchronize ‘python’ * 4.逐行读取:<<<f=open(‘hello.txt’)<<<f.readline() * 5.提取文件的内容:word=open(‘hello.txt’).readlines() word \[‘武汉大学\\n’, ‘xiaodai/n’\] for w in word: print(w) 武汉大学 xiaodai/n * 实战2(中文输入) * f=open(‘whu.txt’,‘w’) f=open(‘whu.txt’,‘w’,encoding=‘utf-8’) f.write(‘WuHan University is most beautiful college\\n’) 43 f.write(‘小代,你过得怎么样?’) 10 f.close() * open(‘whu.txt’).readlines() \[‘WuHan University is most beautiful college\\n’, ‘小代,你过得怎么样?’\] * 实战3(存储内容) * 1.新建文件-写入内容 * x,y,z=1,2,3 l=\[11,22,33\] type(x) <class ‘int’> f=open(‘datafile’,‘w’) f.write(’\{\},\{\},\{\}’.format(x,y,z)) 5 f,write(str(l)) * 2.关闭-保存 * f.close() * 3.读取 * f.close() char=open(‘datafile’).read() char ‘1,2,3\[11, 22, 33\]’ * 4.问题:如何还原内容 * 实战4.二进制存储pickle * 1.建立二进制文件 * d=\{‘a’:1,‘b’:2\} f=open(‘datafile.pkl’,‘wb’) import pickle pickle.dump(d,f) f.close() * 2.查看二进制文件 * open(‘datafile.pkl’,‘rb’).read() b’\\x80\\x03\}q\\x00(X\\x01\\x00\\x00\\x00aq\\x01K\\x01X\\x01\\x00\\x00\\x00bq\\x02K\\x02u.’ * 3.还原内容 * f=open(‘datafile.pkl’,‘rb’) data=pickle.load(f) data \{‘a’: 1, ‘b’: 2\} data\[‘b’\] 2 data.get(‘a’) 1 * 实战5.with自动释放 * with open(‘whu.txt’) as f: for line in f.readlines(): print(line) 输出: WuHan University is most beautiful college 小代,你过得怎么样? ### 汇总 ### * 集合 * 序列 * 可变 * 列表list * 不可变 * 字符串str * 元组tuple * 映射 * 字典表dict * 集合 * 数字 * 整型 * int * bool * 浮点型 * float * Decimal * … * 可调用 * 函数function * 生成器Genergator * 类class * 方法 * 其他 * 文件file * None * 视图 * … * 内部 * Type * … *XMind: ZEN - Trial Version*
相关 中心主题 5.数据类型 元组(tuple) 特点 任意对象的有序集合 通过下标来访问 属于‘’不可变类型‘’ ╰+哭是因爲堅強的太久メ/ 2023年08月17日 16:52/ 0 赞/ 111 阅读
相关 中心主题 5.数据类型 数值 声明、赋值、使用 表达式 显示 ‘0:.2f’.format(4.444) import Dear 丶/ 2023年08月17日 16:18/ 0 赞/ 130 阅读
相关 中心主题 4.链表 概念 顺序表,将元素顺序地存放在-块连续的存储区里,元素间的顺序关系由它们的存储顺序自然表示。 链表,将元素存放在通过链接构造起来的一系列存储 谁践踏了优雅/ 2023年06月08日 14:57/ 0 赞/ 134 阅读
相关 中心主题 18.文件读取 txt def write\_txt(): with open(‘data.txt’,‘w’,encoding=‘utf8’) a 淩亂°似流年/ 2023年06月07日 08:23/ 0 赞/ 34 阅读
相关 中心主题 15.正则表达式 概述 概念 Regular Expression 一种文本模式,描述在搜索文本时要匹配的一个或多个字符 分手后的思念是犯贱/ 2023年06月06日 12:22/ 0 赞/ 27 阅读
相关 中心主题 13.对象持久化 扁平文件 文本文件 使用列表存储出现,把标点符号独立存储问题 使用eval()可以解决上述问题,但是当 ゝ一纸荒年。/ 2023年06月05日 13:59/ 0 赞/ 52 阅读
还没有评论,来说两句吧...