Head Frist Python 读书笔记 第四章 文件与异常
2016-08-18 15:53
543 查看
元组(tuple)
这种数据结构,一旦生成则不能改变,查了一下发现.net中也有这种结构
红字标出的split方法,返回的就是一个元组
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example,
For example:
>>>
If sep is not specified or is
For example:
>>>
pass
用于忽略异常,继续执行之后的代码
这种数据结构,一旦生成则不能改变,查了一下发现.net中也有这种结构
import os print(os.getcwd ()) data=open("sketch.txt") for each_line in data: if each_line.find(":")!=-1: (role,line_spoken)=each_line.split(":",1) print(role,end="") print(" said: ",end="") print(line_spoken,end="") data.close()
红字标出的split方法,返回的就是一个元组
str.
split(sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most
maxsplit+1elements). If maxsplit is not specified or
-1, then there is no limit on the number of splits (all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example,
'1,,2'.split(',')returns
['1', '', '2']). The sep argument may consist of multiple characters (for example,
'1<>2<>3'.split('<>')returns
['1', '2', '3']). Splitting an empty string with a specified separator returns
[''].
For example:
>>>
>>> '1,2,3'.split(',') ['1', '2', '3'] >>> '1,2,3'.split(',', maxsplit=1) ['1', '2,3'] >>> '1,2,,3,'.split(',') ['1', '2', '', '3', '']
If sep is not specified or is
None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a
Noneseparator returns
[].
For example:
>>>
>>> '1 2 3'.split() ['1', '2', '3'] >>> '1 2 3'.split(maxsplit=1) ['1', '2 3'] >>> ' 1 2 3 '.split() ['1', '2', '3']
pass
用于忽略异常,继续执行之后的代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/18/1734a73c0b0edfa9f7efa4dfddcef805.png)
相关文章推荐
- Head First Python 第二章 函数模块&第三章 文件与异常&第四章 持久存储&第五章 处理数据
- Head First Python学习笔记2——文件与异常
- Head Frist Python 读书笔记 列表推导(list comprehension)
- [Head First Python]3. 文件与异常:处理错误
- Head Frist Python 读书笔记 构建发布
- (完)learn python第四章:异常处理机制和文件(第一节:异常处理机制)
- Head Frist Python 读书笔记 第六章 定制数据对象
- HeadFirstPython---------(二)第四章,持久存储,数据保存到文件
- 《Head_First_Python》 3文件与异常
- 《head first python》——文件与异常
- Head Frist Python 读书笔记 第4章 “腌制”数据
- Head First Python之3文件与异常
- Head First Python---------(一)第一章(初识Python__人人都爱列表)、第二章(共享你的代码__函数模块)、第三章(文件与异常__处理错误)
- 《Head First Python》笔记 第三章 文件与异常
- 笔记:HeadFirstPython(3)文件与异常
- Head Frist Python 读书笔记 第五章 处理数据
- Python初入门(三)(Head First Python 第三章 文件与异常)
- Head First Python(文件与异常)
- python 检测文件,有异常信息就发送邮件
- learn with python-第十一章:文件和异常