python基础
本章总结:
- 列表、元组操作
- 字符串操作
- 字典操作
- 集合操作
- 文件操作
- 字符编码与转码
一:列表、元组操作
列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
name = ["guojianyu","wangqin"]#定义列表 print (name[0]) >>'guojianyu' print (name[1]) >> 'wangqin' print (name[-1])#可以看到下标-1对应的是最后一个元素 >> 'wangqin' print (name[-2]) >> 'guojianyu'
通过下标访问列表中的元素,下标从0开始计数。
切片:取多个元素
#__author__:"Jay guo" #__date__:2016/8/24 name = ["guojianyu","wangqin","zhangsan"] print (name[:])#将name全部输出 >>['guojianyu', 'wangqin', 'zhangsan'] print (name[:-1])#代表从下标为0取到-1的前一位,上边说过-1代表最后一个元素 >>['guojianyu', 'wangqin'] print (name[::-1])#反转,第三个参数为步长,不填默认为1 >>['zhangsan', 'wangqin', 'guojianyu'] print (name[::-2])#从右边以第一个位置为起点步长为2 >>['zhangsan', 'guojianyu'] print (name[::2])#可以看到第三个参数为负数时,代表反向。 >>['guojianyu', 'zhangsan']
增:
#__author__:"Jay guo" #__date__:2016/8/24 name = ["guojianyu","wangqin","zhangsan"] name.append("新来的")#追加 print (name) >>['guojianyu', 'wangqin', 'zhangsan', '新来的'] name.insert(1,"love")#第一个参数为插入位置的下标,第二个为插入的内容 print (name) >>['guojianyu', 'love', 'wangqin', 'zhangsan', '新来的']
删:
#__author__:"Jay guo" #__date__:2016/8/24 name = ["guojianyu","wangqin","zhangsan","zhangsan"] name.remove("zhangsan")#删除从左起第一个内容与参数相同的元素 print (name) #>>['guojianyu', 'wangqin', 'zhangsan'] a = name.pop(2)#删除下标为2的元素并返回删除元素的内容 print ("a = ",a," name = ",name) #>>a = zhangsan name = ['guojianyu', 'wangqin'] del name[0]#删除下标为0的元素 print (name) >>['wangqin'] del name#删除链表
改:
#__author__:"Jay guo" #__date__:2016/8/24 name = ["guojianyu","wangqin","zhangsan","zhangsan"] name[2] = "换人了" print (name) >>['guojianyu', 'wangqin', '换人了', 'zhangsan']
查:
name = ["guojianyu","wangqin","zhangsan","zhangsan"] b = name.index("zhangsan")#根据内容找索引,注意这个方法只是返回第一查找内容的索引 print (b) >> 2
元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
name = ("guojianyu","wangqin","zhangsan","zhangsan") #元组的创建
二:字符串的操作
字符串与元组一样是不允许修改的。
#__author__:"Jay guo" #__date__:2016/8/24 name = "guojianyugg" b = name.count("g")#count方法是计算参数在变量中出现的次数 print (b) >>3 print (name[0]) >>g print (name[1]) >>u print (name[2]) >>o print (name.title()) >>Guojianyugg count= "123456" print (count.isdigit())#.isdigit是判断字符串是否像一个数字 >>True count = "123456a" print (count.isdigit()) >>False
因为字符串的内置方法众多就不一一列举,请自行查阅,在元组,列表,字符串中许多方法可以共用
三:字典的操作
字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。
#__author__:"Jay guo" #__date__:2016/8/24 dict = {"nan":"guo","nv":"wang"} #字典的格式
字典的特性:
- dict是无序的
- key必须是唯一的,so 天生去重
增:
#__author__:"Jay guo" #__date__:2016/8/24 dict = {"nan":"guo","nv":"wang"} #字典的格式 dict["center"] = "it"#字典中的增 print (dict) >>{'nv': 'wang', 'nan': 'guo', 'center': 'it'}
删:
#__author__:"Jay guo" #__date__:2016/8/24 dict = {"nan":"guo","nv":"wang","center":"it"} #字典的格式 dict.pop("center")#删除key为"center"的元素 print (dict) >>{'nan': 'guo', 'nv': 'wang'} dict.popitem()#随机删除字典中的一个元素 print (dict)
改:
#__author__:"Jay guo" #__date__:2016/8/24 dict = {"nan":"guo","nv":"wang","center":"it"} #字典的格式 dict["nan"] = "guojianyu" print (dict) >>{'nv': 'wang', 'center': 'it', 'nan': 'guojianyu'}
查:
>>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
>>>
>>> "stu1102" in info #标准用法
True
>>> info.get("stu1102") #获取
'LongZe Luola'
>>> info["stu1102"] #同上,但是看下面
'LongZe Luola'
>>> info["stu1105"] #如果一个key不存在,就报错,get不会,不存在只返回None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'stu1105'
多级字典嵌套及操作:
av_catalog = { "欧美":{ "www.youporn.com": ["很多免费的,世界最大的","质量一般"], "www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"], "letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"], "x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"] }, "日韩":{ "tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"] }, "大陆":{ "1024":["全部免费,真好,好人一生平安","服务器在国外,慢"] } } av_catalog["大陆"]["1024"][1] += ",可以用爬虫爬下来" print(av_catalog["大陆"]["1024"]) #ouput ['全部免费,真好,好人一生平安', '服务器在国外,慢,可以用爬虫爬下来']
遍历字典:
#__author__:"Jay guo" #__date__:2016/8/24 dict = {"nan":"guo","nv":"wang","center":"it"} #字典的格式 for i in dict:#输出dict中的所有key值 print (i) >> nan center nv for i in dict:#建议使用这种方式进行遍历 print (i,dict[i]) >> nan guo center it nv wang for i,j in dict.items(): print (i,j) >> nan guo center it nv wang
四:集合操作
集合是一个无序的,不重复的数据组合,它的主要作用如下:
- 去重,把一个列表变成集合,就自动去重了
- 关系测试,测试两组数据之前的交集、差集、并集等关系
1.集合的创建:
man = ("attractive","perfect","brilliant") guo = set(man)#创建集合 print (guo) >> {'perfect', 'brilliant', 'attractive'}
2.集合的特性:
#__author__:"Jay guo" #__date__:2016/9/5 man = ("attractive","perfect","brilliant","perfect")#元组中有两个perfect guo = set(man) print (guo) >>{'perfect', 'attractive', 'brilliant'}#可以看到两次输出结果并不一致,也就说明了集合的无序性 >>{'attractive', 'brilliant', 'perfect'}#定义的元组有两个“perfect”但转换成集合时将重复的去除,则说明集合的去重性
3.集合的操作:
(1)集合删除操作:
#__author__:"Jay guo" #__date__:2016/9/5 man = ("attractive","perfect","brilliant","perfect")#元组中有两个perfect guo = set(man) print (guo) >>{'perfect', 'brilliant', 'attractive'} #现在我们要删除一个已知的元素 guo.remove("perfect") print (guo) >>{'brilliant', 'attractive'} #删除整个集合 del guo print (guo) >>NameError: name 'guo' is not defined #可以看到报错的内容为没有定义则说明我们的集合删除成功
#清空集合
guo.clear()
print (guo)
>>set()
(2)集合中添加元素:
#__author__:"Jay guo" #__date__:2016/9/5 man = ("attractive","perfect","brilliant","perfect")#元组中有两个perfect guo = set(man) print (guo) # >>{'perfect', 'brilliant', 'attractive'} #现在我们给guo中添加一个元素: guo.add("wangqin") print (guo) >>{'perfect', 'attractive', 'brilliant', 'wang'}#可以看到"wangqin"元素添加进入集合中 #集合添加多个元素 women = ("beautiful","lovely","soft") guo.update(women) print (guo) >>{'wangqin', 'brilliant', 'lovely', 'soft', 'perfect', 'attractive', 'beautiful'}#可以看到多个元素添加成功
五: 文件操作
对文件操作流程
- 打开文件,得到文件句柄并赋值给一个变量
- 通过句柄对文件进行操作
- 关闭文件
现有文件如下
Somehow, it seems the love I knew was always the most destructive kind 不知为何,我经历的爱情总是最具毁灭性的的那种 Yesterday when I was young 昨日当我年少轻狂 The taste of life was sweet 生命的滋味是甜的 As rain upon my tongue 就如舌尖上的雨露 I teased at life as if it were a foolish game 我戏弄生命 视其为愚蠢的游戏 The way the evening breeze 就如夜晚的微风 May tease the candle flame 逗弄蜡烛的火苗 The thousand dreams I dreamed 我曾千万次梦见 The splendid things I planned 那些我计划的绚丽蓝图 I always built to last on weak and shifting sand 但我总是将之建筑在易逝的流沙上 I lived by night and shunned the naked light of day 我夜夜笙歌 逃避白昼赤裸的阳光 And only now I see how the time ran away 事到如今我才看清岁月是如何匆匆流逝 Yesterday when I was young 昨日当我年少轻狂 So many lovely songs were waiting to be sung 有那么多甜美的曲儿等我歌唱 So many wild pleasures lay in store for me 有那么多肆意的快乐等我享受 And so much pain my eyes refused to see 还有那么多痛苦 我的双眼却视而不见 I ran so fast that time and youth at last ran out 我飞快地奔走 最终时光与青春消逝殆尽 I never stopped to think what life was all about 我从未停下脚步去思考生命的意义 And every conversation that I can now recall 如今回想起的所有对话 Concerned itself with me and nothing else at all 除了和我相关的 什么都记不得了 The game of love I played with arrogance and pride 我用自负和傲慢玩着爱情的游戏 And every flame I lit too quickly, quickly died 所有我点燃的火焰都熄灭得太快 The friends I made all somehow seemed to slip away 所有我交的朋友似乎都不知不觉地离开了 And only now I'm left alone to end the play, yeah 只剩我一个人在台上来结束这场闹剧 Oh, yesterday when I was young 噢 昨日当我年少轻狂 So many, many songs were waiting to be sung 有那么那么多甜美的曲儿等我歌唱 So many wild pleasures lay in store for me 有那么多肆意的快乐等我享受 And so much pain my eyes refused to see 还有那么多痛苦 我的双眼却视而不见 There are so many songs in me that won't be sung 我有太多歌曲永远不会被唱起 I feel the bitter taste of tears upon my tongue 我尝到了舌尖泪水的苦涩滋味 The time has come for me to pay for yesterday 终于到了付出代价的时间 为了昨日 When I was young 当我年少轻狂
基本操作
|
1
2
3
4
5
6
7
8
|
f = open('lyrics') #打开文件first_line = f.readline()print('first line:',first_line) #读一行print('我是分隔线'.center(50,'-'))data = f.read()# 读取剩下的所有内容,文件大时不要用print(data) #打印文件f.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
with语句
为了避免打开文件后忘记关闭,可以通过管理上下文,即:
|
1
2
3
|
with open('log','r') as f: ... |
如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。
在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:
with open('log1') as obj1, open('log2') as obj2: pass
六:字符编码与转码
需知:
1.在python2默认编码是ASCII, python3里默认是utf-8
2.unicode 分为 utf-32(占4个字节),utf-16(占两个字节),utf-8(占1-4个字节), so utf-8就是unicode
3.在py3中encode,在转码的同时还会把string 变成bytes类型,decode在解码的同时还会把bytes变回string


浙公网安备 33010602011771号