Python自学-Day3 自记笔记
本日内容
一、作业解答
二、集合
三、文件操作
四、
五、
六、
七、
八、
九、
十、
一、作业
作业:
购物车优化:
用户入口:商品信息存在文件里,购买完成后,信息存在文件里,余额也记录,下次不用输入工资
商家入口:可以添加商品,修改商品价格
二、集合
如何把一个列表中重复的内容去掉?
目前学习的办法是这样:
1 # Auther: Tank Jarvis 2 a = [1,2,3,1] 3 b = [] 4 5 for i in a: 6 if i not in b: 7 b.append(i) 8 print(a) 9 print(b) 10 11 Result: 12 [1,2,3,1] 13 [1,2,3]
这个办法非常笨,还消耗内存,因为新生成了列表,那简单的办法就是集合,这也是集合的一个特点,
另一个特点,怎么按照特性去取一些值?也就是关系测试。
集合特点:
1.关系测试
1 # Auther: Tank Jarvis 2 #集合关系测试 3 list_1 = [1,4,5,7,3,6,7,9] 4 #变成集合 5 list_1 = set(list_1) 6 print(list_1,type(list_1)) 7 #集合也是用大括号表示,但是没有key-value,不是字典,而且集合是无序的 8 9 list_2 = set([2,6,0,66,22,8,4]) 10 print(list_1,list_2) 11 #交集4、6 12 print(list_1 & list_2) 13 print(list_1.intersection(list_2)) 14 #并集 15 print(list_1 | list_2) 16 print(list_1.union(list_2)) 17 #差集,在list_1并且不在list_2 18 print(list_1 - list_2) 19 print(list_1.difference(list_2)) 20 #子集 21 print(list_1.issubset(list_2)) 22 #父集 23 list_3 = set([1,3,7]) 24 print(list_3.issuperset(list_1)) 25 print(list_3.issubset(list_1)) 26 #对称差集==并集-交集 27 print(list_1 ^ list_2) 28 print(list_1.symmetric_difference(list_2)) 29 #如果两个集合没有交集,返回为True,有交集返回False 30 list_4 =set([2,4,6]) 31 print(list_2.isdisjoint(list_1)) 32 print(list_4.isdisjoint(list_3))
2.其他语法
1 list_1 = set([1,3,5,7,9]) 2 #添加一项 3 list_1.add(11) 4 print(list_1) 5 #添加多项 6 list_1.update([13,15,17]) 7 print(list_1) 8 #删除 9 list_1.remove(5) 10 print(list_1) 11 list_1.pop() #任意删除 12 print(list_1) 13 #看长度 14 print(len(list_1)) 15 #删除一个不存在的数据也不会报错 16 list_1.discard("ddd") 17 #浅复制 18 list_2 = list_1.copy()
三、文件操作
对文件的操作:
1.找到并打开文件
2.操作文件
3.关闭文件
1 # Auther: Tank Jarvis 2 """ 3 data = open("yesterday",encoding="utf-8").read() 4 #因为windows系统默认是gbk读取文件,python3默认是utf-8读取 5 #文件,所以要用python打开文件,需要指定字符编码 6 print(data) 7 """ 8 #把内存中的文件内容赋给一个变量f,赋予的内容也叫文件句柄 9 #包括文件名,字符集,大小,在硬盘上的起始位置等 10 #读 r 有读就不能写 11 f = open("yesterday","r",encoding="utf-8") 12 data = f.read() 13 data2 = f.read() #如果再读一遍 14 print(data) 15 print("-------data2-------",data2) 16 #你会发现data2并没有任何显示,为什么? 17 #因为文件read是有留存指针光标的特点,第一遍read已经到最后了, 18 #再读一遍就肯定是空的了,后面学习怎么移动指针光标 19 20 #写 w 有写就不能有读,而且只要w执行后,就会洗刷文件 21 f2 = open("yesterday2","w",encoding="utf-8") 22 f2.write("我爱北京天安门,\n") 23 f2.write("天安门上太阳升。") 24 25 #追加 a 不能读 26 f3 = open("yesterday2","a",encoding="utf-8") 27 f3.write("\n伟大领袖~~~") 28 29 f.close() 30 f2.close() 31 f3.close() 32 #别忘了关闭之前open的文件
如果有更细节的操作呢?
1 # Auther: Tank Jarvis 2 #读这个文件,在第4行显示其他内容 3 f = open("yesterday","r",encoding="utf-8") 4 for i,j in enumerate(f.readlines()): #这里的f.readlines()的输出是列表,i为脚标,j为内容 5 if i == 3: 6 print("This is 4th line.") 7 else: 8 print(j.strip()) #strip去掉空格和换行 9 f.close()
这里要注意,f.readlines()只能读小文件,因为这个命令把所有文件都放置到内存中,这时就需要更能节省内存空间的命令
可以保持内存中只保持一行内容,这样就可以处理大文件了:(可以忘记readline了)
1 f = open("yesterday","r",encoding="utf-8") 2 count = 0 3 4 for line in f: 5 if count == 3: 6 print("this is 4th line") 7 count += 1 8 continue 9 print(line.strip()) 10 count +=1
11 f.close()
指针调整,以及其他功能
1 f = open("yesterday","r",encoding="utf-8") 2 print(f.tell()) 3 print(f.readline()) 4 print(f.tell()) #会显示当前指针位置 5 f.seek(0) #调整指针到指定位置 6 print(f.tell()) 7 8 f.close()
1 f = open("yesterday","r",encoding="utf-8") 2 print(f.encoding) #打印当前字符集 3 f.flush() 4 #刷新,如果我以写的模式写一行, 5 # 我们认为已经写到硬盘上去了,但是如果断电了, 6 # 那这行就可能没写进去,因为这一行还在缓存中, 7 # 因为硬盘速度慢,为了解决这个问题, 8 # 我们就可以用flush()把缓存的东西都刷到硬盘上去 9 f.truncate(10)#截断,从头开始截字符,a模式下可用 10 11 f.close()
当然文件处理不只有w,r,a,还有r+,w+,a+
1 # Auther: Tank Jarvis 2 f = open("yesterday","r+",encoding="utf-8") 3 #读写模式可以读可以追加,追加读写模式a+和r+功能一样 4 print(f.readline()) 5 print(f.readline()) 6 print(f.readline()) 7 print(f.tell()) 8 f.write("\n, ------------diao---------") 9 print(f.readline()) 10 11 f.close() 12 13 f2 = open("yesterday","w+",encoding="utf-8") 14 #写读模式没什么乱用,和写模式一样,没区别 15 print(f2.readline()) 16 print(f2.readline()) 17 print(f2.readline()) 18 f2.write("\n==========diao======") 19 print(f2.readline()) 20 f2.close()
二进制文件的读写
1 f = open("yesterday2","rb") 2 #rb模式,是读取二进制文件所使用的,例如视频就是二进制的 3 #有两种情况会使用rb,1.网络传输ftp;2.迅雷下载的视频文件 4 print(f.readline()) 5 print(f.readline()) 6 print(f.readline()) 7 f.close() 8 #wb模式就是可写, 9 f2 = open("yesterday2","wb") 10 f2.write("hello binary\n".encode()) 11 f2.close()
总结下:
打开文件的模式有:
- r,只读模式(默认)。
- w,只写模式。【不可读;不存在则创建;存在则删除内容;】
- a,追加模式。【可读; 不存在则创建;存在则只追加内容;】
"+" 表示可以同时读写某个文件
- r+,可读写文件。【可读;可写;可追加】
- w+,写读
- a+,同a
"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)
- rU
- r+U
"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)
- rb
- wb
- ab
其他用法:
def close(self): # real signature unknown; restored from __doc__ """ Close the file. A closed file cannot be used for further I/O operations. close() may be called more than once without error. """ pass def fileno(self, *args, **kwargs): # real signature unknown """ Return the underlying file descriptor (an integer). """ pass def isatty(self, *args, **kwargs): # real signature unknown """ True if the file is connected to a TTY device. """ pass def read(self, size=-1): # known case of _io.FileIO.read """ 注意,不一定能全读回来 Read at most size bytes, returned as bytes. Only makes one system call, so less data may be returned than requested. In non-blocking mode, returns None if no data is available. Return an empty bytes object at EOF. """ return "" def readable(self, *args, **kwargs): # real signature unknown """ True if file was opened in a read mode. """ pass def readall(self, *args, **kwargs): # real signature unknown """ Read all data from the file, returned as bytes. In non-blocking mode, returns as much as is immediately available, or None if no data is available. Return an empty bytes object at EOF. """ pass def readinto(self): # real signature unknown; restored from __doc__ """ Same as RawIOBase.readinto(). """ pass #不要用,没人知道它是干嘛用的 def seek(self, *args, **kwargs): # real signature unknown """ Move to new file position and return the file position. Argument offset is a byte count. Optional argument whence defaults to SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values are SEEK_CUR or 1 (move relative to current position, positive or negative), and SEEK_END or 2 (move relative to end of file, usually negative, although many platforms allow seeking beyond the end of a file). Note that not all file objects are seekable. """ pass def seekable(self, *args, **kwargs): # real signature unknown """ True if file supports random-access. """ pass def tell(self, *args, **kwargs): # real signature unknown """ Current file position. Can raise OSError for non seekable files. """ pass def truncate(self, *args, **kwargs): # real signature unknown """ Truncate the file to at most size bytes and return the truncated size. Size defaults to the current file position, as returned by tell(). The current file position is changed to the value of size. """ pass def writable(self, *args, **kwargs): # real signature unknown """ True if file was opened in a write mode. """ pass def write(self, *args, **kwargs): # real signature unknown """ Write bytes b to file, return number written. Only makes one system call, so not all of the data may be written. The number of bytes actually written is returned. In non-blocking mode, returns None if the write would block. """ pass
文件的修改:
1.类似于VIM一样把文件都加载到内存里修改,在保存回源文件,需要把整个文件都加载到内存,不适于大文件;
2.保存成一个新文件保存回硬盘
1 # Auther: Tank Jarvis 2 f1 = open("yesterday","r",encoding="utf-8") 3 f2 = open("yesterday2","r+",encoding="utf-8") 4 for line in f1: 5 if "生命的滋味是甜的" in line: 6 line = line.replace("生命的滋味是甜的","生命的滋味是苦的") 7 f2.write(line) 8 f1.close() 9 f2.close()
with语句:
1 f1 = open("yesterday","r",encoding="utf-8") 2 with open("yesterday","r",encoding="utf-8") as f2:
这两行作用是一样的,只不过with语句不用再close了,with执行完了会自动关闭文件
1 with open('log1') as f1, open('log2') as f2:
with也可以同时打开多个文件,但是python的语法要求:希望使用者代码清晰,多个文件分行打开
1 with open("yesterday","r",encoding="utf-8") as f2,\ 2 open("yesterday","r",encoding="utf-8") as f3:
这样比较规范。
文件操作作业:
修改haproxy配置文件
需求:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1、查 输入:www.oldboy.org 获取当前backend下的所有记录 2、新建 输入: arg = { 'bakend': 'www.oldboy.org', 'record':{ 'server': '100.1.7.9', 'weight': 20, 'maxconn': 30 } } 3、删除 输入: arg = { 'bakend': 'www.oldboy.org', 'record':{ 'server': '100.1.7.9', 'weight': 20, 'maxconn': 30 } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enable stats uri /admin stats auth admin:1234 frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if www backend www.oldboy.org server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
四、字符编码与转码
详细文章:
http://www.cnblogs.com/yuanchenqi/articles/5956943.html
http://www.diveintopython3.net/strings.html
需知:
1.在python2默认编码是ASCII, python3里默认是unicode
2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节), so utf-16就是现在最常用的unicode版本, 不过在文件里存的还是utf-8,因为utf8省空间
3.在py3中encode,在转码的同时还会把string 变成bytes类型,decode在解码的同时还会把bytes变回string
只适用于Python2:
encode encode
GBK<----------unicode---------->UTF-8
GBK --------->unicode<----------UTF-8
decode decode
GBK需要转换为UTF-8的格式流程:
1.首先通过编码 decode 转换为unicode编码
2.然后通过编码 encode 转换为UTF-8的编码
UTF-8需要转换为GBK格式流程:
1.首先通过编码 decode 转换为Unicode编码
2.然后通过编码 encode 转换为GBK的编码
1 #-*-coding:gb2312 -*- #这个也可以去掉 2 3 import sys 4 print(sys.getdefaultencoding()) 5 6 7 msg = "我爱北京天安门" 8 #msg_gb2312 = msg.decode("utf-8").encode("gb2312") 9 msg_gb2312 = msg.encode("gb2312") #默认就是unicode,不用再decode,喜大普奔 10 gb2312_to_unicode = msg_gb2312.decode("gb2312") 11 gb2312_to_utf8 = msg_gb2312.decode("gb2312").encode("utf-8") 12 13 print(msg) 14 print(msg_gb2312) 15 print(gb2312_to_unicode) 16 print(gb2312_to_utf8)
五、