01 昨日内容回顾
数据类型补充:
str <---> list split join
list <---> set set(list) list(set())
list <---> tuple tuple(list) list(tuple)
int str bool
bool False: "" 0 [] () set() {} None

tuple:
(1) 与元素数据类型相同

list:在循环一个列表时,最好不要改变列表的大小,会影响结果。
倒叙循环:
for i in range(len(l1)-1,-1,-1):
pass

dict:在循环一个字典时,不能改变字典的大小,会报错。

is id ==
== 比较的是两边的数值是否相同
id() 对象的内存地址
内存地址相同:变量的在内存中的指向是同一个内存地址 同一个内容。
is 比较的是内存地址相同,是否是同一个人。

代码块:
一个文件。 命令行的一行就是一个代码块。

同一代码块中的缓存机制:
适应的数据类型:str int bool
意义:
1,节省内存
2,提高性能。

小数据池:
适应的数据类型:str(一定范围) int(-5~256) bool
意义:
1,节省内存
2,提高性能。
编码进阶:
ascii unicode utf-8 gbk
1,不同编码之间不能相互识别。(乱码或者出错)
2,网络传输和数据存储不能是通过unicode的编码方式的01010101.

python3x:
str:内部编码方式:unicode

不能直接将字符串用于网络传输和数据存储。

bytes:内部编码方式:非unicode

英文:
str:'alex'
内存编码:unicode

bytes:b'alex'
内存编码:非unicode

中文:
str:'太白'
内存编码:unicode

bytes:b'\ex3\ex3\ex3\ex3\ex3\ex3\'
内存编码:非unicode

unicode ----> b1 = s1.encode('gbk')
gbk --------> b1.decode('gbk')
unicode ----> s1.encode('utf-8')
utf-8 --------> b1.decode('utf-8')

b1 gbk的bytes类型
gbk -----> utf-8
b1.decode('gbk').encode('utf-8')

深浅copy:
l1 = [1,2,3,[22,]]
l2 = l1.copy()
浅copy:在内存中创建一个新的列表,但是内容沿用原列表的内容。
import copy
l1 = [1,2,3,[22,]]
l2 = copy.deepcopy(l1)
深copy:在内存中创建一个新的列表,但是列表中的不可变的数据类型沿用原来的,可变的数据类型创建新的内存地址。

02 作业讲解

03 文件操作
初识
美女护士教师空姐的联系方式.txt
如果想要通过python代码操作这个文件:必须的三要素:
path:文件的路径。
mode:r w r+ w+ a ....
encoding: 编码方式

报错原因:
1,路径错误。 \与后面的那个字符具有了特殊意义。
解决方式:
r'd:\美女护士空姐联系方式.txt' 在路径最前面+ r
'd:\\美女护士空姐联系方式.txt' 第一个\对第二个进行转义。
2,Unicodedecodeerror: 编码问题。
encoding='utf-8' 打开文件的编码与文件存储时的编码不一致。

3, encoding 只是声明了此文件的需要用什么编码本编码解码而已。

4,路径:
绝对路径:从磁盘(根目录)开始,直到找到你的文件。
相对路径:当前路径(当前文件夹)找到的文件。

读(r rb r+ r+b)

写 (w wb w+ w+b)

追加 (a ab a+ a+b)

以上总结:
最常用的 r r+ 其次 rb w wb a
带b的模式都是操作的非文本类的文件:图片 音频,视频。

文件操作的其他功能
readable writable flush tell seek truncate

文件的改的操作

处理文件的另一种方式:
with open()

# 03 文件操作初识以及 读模式

# f1 = open('e:\molinbin.txt',encoding='utf-8',mode='r')# content = f1.read()# print(content)# print(f1.read())#有返回值,可以直接用# f1.close()

'''f1 f file file_handler ,f_h.... 文件句柄open() 内置函数 这个函数实际上是调用的操作系统的对文件操作的功能,windows:默认的编码方式gbk.linux: 默认的编码方式utf-8.IOS:默认的编码方式utf-8.接下来你对文件进行的任何操作,都需借助文件句柄操作。f.close()''''''1, 打开文件产生文件句柄(path, encoding mode)。2,对文件句柄进行操作。3,关闭文件句柄。'''

# f1 = open(r'e:\molinbin.txt',encoding='utf-8',mode='r')##在路径最前面+ r# content = f1.read()# print(content)# f1.close()

# f1 = open('file1',encoding='utf-8',mode='r')# content = f1.read()# print(content)# f1.close()

# r 下的五种读取方式# 1,read() 全读出来

# 2,read(n) 读n个字符# f1 = open('file1',encoding='utf-8',mode='r')# content = f1.read(4)# print(content)# f1.close()

'''在r 模式下 按照字符读取在rb 模式下按照字节读取'''# 3 按行读取 readline()##有返回值# f1 = open('file1', encoding='utf-8', mode='r')# content = f1.readline()# content = f1.readline()# content = f1.readline()# print(content,type(content))# print(f1.readline())# f1.close()

# 4 readlines() 返回一个列表,列表中的每个元素是原文件的每行的数据。# f1 = open('file1', encoding='utf-8', mode='r')# content = f1.readlines()# print(content)# f1.close()

#for循环# f1 = open('file1',encoding='utf-8')# for line in f1:#     print(line.strip)# f1.close()

# rb 模式# b模式操作的文件是非文字类的文件:图片,视频,音频等等。# read() read(n) readline() readlines() for 循环# f = open('2.png',mode='rb')# print(f.read())# f.close()

# f = open('2.png',mode='rb')# print(f.read(3))# 按照字节# f.close()

'''在r 模式下 按照字符读取在rb 模式下按照字节读取'''

# r+ 读写模式# 先读后写(后追加)   #不读就往光标位置添加# f1 = open('file1', encoding='utf-8', mode='r+')# content = f1.read()# print(content)# f1.write('深圳')# f1.close()

# 如果我就要先写后读呢?# 光标,指针

# f1 = open('file1',encoding='utf-8',mode='r+')# f1.seek(0,2)# 把光标调整到最后,2表示最后# f1.write('北海')# f1.seek(0)# content = f1.read()# print(content)# f1.close()

# 在r+ 读写模式下,应该先读后写入。

# 04 文件操作:写模式

# 写# w wb w+ w+b# 没有文件创建文件写入,有文件清空原文件内容写入新内容。# w模式 必须是以字符串的内容写入# f1 = open('file2',encoding='utf-8',mode='w')# f1.write('北海')# f1.write('北海')#清空一次# f1.write('北海')# f1.write('北海')# f1.write('北海')# f1.close()

# f1 = open('2.png',mode='rb')# content = f1.read()# 以bytes读取原图片获取数据# f1.close()# print(content)

# f1 = open('2.png',mode='rb')# content = f1.read()  # 以bytes读取原图片获取数据# f1.close()# f2 = open('1.png',mode='wb')# f2.write(content)# 将数据写入到一个新文件图片# f2.close()# 完成了对原图片的复制。

# w+ :写读模式# f1 = open('file2',encoding='utf-8',mode='w+')# f1.write('78945613')# f1.seek(0)# print(f1.read())# f1.close()

# 05 文件操作:追加模式# 追加模式:a ab a+ a+b

# 没有文件创建文件追加内容,有文件在原文件的末尾追加新内容# f1 = open('file3', encoding='utf-8', mode='a')# f1.write('老男孩教育!')# f1.close()

# f1 = open('file3', encoding='utf-8', mode='a')# f1.write('太白金星')# f1.close()

# 06 文件操作:其他操作方法# f2 = open('file2',encoding='utf-8',mode='w')# f2.write('guangxinanning')# f2.flush()# 强制保存 相当于ctrl + s# f2.close()

# readable writable 判断一个文件句柄是否可读,可写。# f2 = open('file2',encoding='utf-8',mode='w')# f2.write('广西南宁')# print(f2.readable())# if f2.readable():#     f2.read()# f2.close()

# seek tell 按照字节去调整读光标位置# f1 = open('file2',encoding='utf-8')# # ret = f1.read()# print(f1.tell())#获取当前指针位置# f1.seek(3)# print(f1.read())# f1.close()

# truncate() 只能在可写的模式下 截取原文件。# 只能从头截取,不能调整光标截取一部分。

# f1 = open('file2',encoding='utf-8',mode='r+')# f1.seek(6)# ret = f1.turencate(4)# print(ret)# f1.close()

# 不能在w模式下使用truncate# f1 = open('file2', encoding='utf-8',mode='w')# f1.truncate(6)# f1.close()

# 总结:readable writable flush tell seek  truncate

# 07 文件操作:文件的改

#另一种打开文件的操作方式:#1,自动关闭文件句柄# with open('file1',encoding='utf-8') as f1:#     content = f1.read()#     print(content)

# with open('file1',encoding='utf-8') as f1, \#         open('file2', encoding='utf-8',mode='w') as f2:#     print(f1.read())#     f2.write('666')

# with open('file1',encoding='utf-8') as f1:#     f1.read()#     with open('file1', encoding='utf-8',mode='w') as f2:#         f2.write('5555555')

# 文件的改:# 方法一:# 1,以读的模式打开原文件,产生文件句柄f1# 2, 以写的模式打开新文件,产生文件句柄f2.# import os# with open('log',encoding='utf-8') as f1,\#     open('log.bak',encoding='utf-8',mode='w') as f2:#     # 3,读取原文件 将原文件的内容改写成新内容写入新文件。#     old_content = f1.read()#     new_content = old_content.replace('alex', 'SB')#     f2.write(new_content)## # 4,删除原文件。# os.remove('log')# # 5,将新文件重命名成原文件。# os.rename('log.bak','log')

# 升级版# import os# with open('log',encoding='utf-8') as f1,\#     open('log.bak',encoding='utf-8',mode='w') as f2:#     # 3,读取原文件 将原文件的内容改写成新内容写入新文件。#     for line in f1:#         new_line = line.replace('SB', 'alex')#         f2.write(new_line)# # 4,删除原文件。# os.remove('log')# # 5,将新文件重命名成原文件。# os.rename('log.bak','log')

day_07_python_1124的更多相关文章

随机推荐

  1. MVC路由 路由的三种扩展 替换MVC内置的Handler

    Global.asax 是 程序入口文件 路由配置   为什么localhost:8088/Home/Index/1 能返问到我们写的 会去掉前缀跟端口号  变成Home/Index/1 用这个跟路由 ...

  2. 日志log4cxx 封装、实例讲解、配置文件log4cxx.properties

    日志log4cxx 封装.实例讲解.配置文件log4cxx.properties 1. 日志作用 程序运行过程中,需要记录程序中的运行状况,方便排查问题,记录数据.可以根据日志的记录快速定位错误发生的 ...

  3. Lua和C++交互 学习记录之八:C++类注册为Lua模块

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...

  4. Codeforces 1006 F - Xor-Paths

    F - Xor-Path 思路: 双向搜索dfs 如果普通的搜索复杂度是n 那么双向搜索复杂度是√n 代码: #include<bits/stdc++.h> using namespace ...

  5. legend2---开发日志4(常用的链接传值方式有哪些)

    legend2---开发日志4(常用的链接传值方式有哪些) 一.总结 一句话总结:常用的其实就是get和post,不过有具体细分 a标签 post表单 js方式拼接url 1.js正则尽量少匹配的符号 ...

  6. 雷林鹏分享:jQuery EasyUI 窗口 - 自定义窗口工具栏

    jQuery EasyUI 窗口 - 自定义窗口工具栏 默认情况下,窗口(window)有四个工具:collapsible.minimizable.maximizable 和 closable.比如我 ...

  7. python hashable

    判断一个对象是否hashable: hash(obj) 或 obj.__hash__() ,返回 hash 值 hashable 的有: int / float / tuple / str/  obj ...

  8. LeetCode--011--盛最多水的容器(java)

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  9. EventBus 3.0使用相关

    一 引入方法 可以去github的官网中下载EventBus的相关资源  地址:https://github.com/greenrobot/EventBus 当然还有他的官方网站 http://gre ...

  10. Spring Batch Bean 校验 API 支持

    这个发布版本带来了一个新的  ValidatingItemProcessor 实现,这个实现被称为 BeanValidatingItemProcessor.能够让你使用 Bean Validation ...