解读The Python Tutorial(八)

来源:互联网 发布:js生成年月日时分秒 编辑:程序博客网 时间:2024/05/03 14:28

输入与输出

格式化输出

读写文件

open

open用于返回一个文件对象。

open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

file是字符串文件名,可以是当前目录的相对路径,也可以是绝对路径。
mode是可选参数也是字符串:

‘r’ open for reading (default) 第二个参数省略时默认r
‘w’ open for writing, truncating the file first 存在同名文件会被先删除
‘x’ open for exclusive creation, failing if the file already exists
‘a’ open for writing, appending to the end of the file if it exists
‘b’ binary mode 文件内容被当成字节对象,以二进制模式处理。无需解码,也不应该指明编码。
‘t’ text mode (default) 文件内容被当成str对象处理,通过encoding指明编码方式,自动解码。
‘+’ open a disk file for updating (reading and writing) 列如r+是读写。

buffering
缓冲,是个整数。0代表关闭缓冲(仅二进制模式下才允许),1代表选择缓冲(仅文本模式下允许),和其他整数。二进制缓冲通过是4096或8192字节。

encoding
编码方式,仅文本模式下可用

errors
仅文本模式下可用,指明编码或解码出错时如何处理:
‘strict’ 引发 ValueError exception 和默认值None效果一样。
‘ignore’ 忽略错误,可能导致数据丢失。
‘replace’ 以替换符(如‘?’)插入到错误数据所在位置。
‘surrogateescape’ will represent any incorrect bytes as code points in the Unicode Private Use Area ranging from U+DC80 to U+DCFF. These private code points will then be turned back into the same bytes when the surrogateescape error handler is used when writing data. This is useful for processing files in an unknown encoding.
‘xmlcharrefreplace’ is only supported when writing to a file. Characters not supported by the encoding are replaced with the appropriate XML character reference &#nnn;.
‘backslashreplace’ replaces malformed data by Python’s backslashed escape sequences.
‘namereplace’ (also only supported when writing) replaces unsupported characters with \N{…} escape sequences.

newline
换行(Unix上的\n、Windows上的\r\n)取值可以是None, ”, ‘\n’, ‘\r’, and ‘\r\n’ ,仅文本模式下可用。
1,当读取内容,且None时,但凡遇到’\n’, ‘\r’, and ‘\r\n’都认为读到换行符,且把’\n’, ‘\r’, and ‘\r\n’统一转换成’\n’进行读取。
2,当读取内容,且”时,和None一样,只是不转换换行符,原样读取。
3,当读取内容,且’\n’ 时,仅’\n’才认为是换行符,原样读取’\n’。
4,当读取内容,且’\r’ 时,仅’\r’才认为是换行符,原样读取’\r’。
5,当读取内容,且’\r\n’ 时,仅’\r\n’才认为是换行符,原样读取’\r\n’。

6,当写入内容,且None时,’\n’被转成系统平台默认的换行符进行输出。
7,当写入内容,且” or ‘\n’时,不会转换,直接输出。
8,当写入内容,且’\r’时,’\n’被转成’\r’进行输出。
9,当写入内容,且’\r\n’时,’\n’被转成’\r\n’进行输出。

由于存在换行符的替换,会破坏EXEC,JPG等文件。所以这些文件需要用二进制模式处理。

文件的读取

>>> with open('workfile') as f:      #建议用with关键字...     read_data = f.read()>>> f.closed #由于用了with,其实无需调用closedTrue

call f.read(size) returns some data as a string (in text mode) or bytes object (in binary mode)
size是可选的,如果省略或为负数则读取整个文件,否则读取最多size个字节。

>>> f.read()'This is the entire file.\n'>>> f.read() #如果读到末尾返回空字符串''。''

f.readline() 读取一行,附带有一个换行符。如果仅仅读到一个换行符说明读到了一个空行。如果读到的行没有换行符说明读到最后一行,且这行结束后刚好没有换行符。如果读到空字符串”表示已经读到末尾。

>>> f.readline()'This is the first line of the file.\n'>>> f.readline()'Second line of the file\n'>>> f.readline()''#又或者把end指定空串,因为读到的行自带换行符。>>> for line in f:...     print(line, end='')...This is the first line of the file.Second line of the file

f.write(string)把字符串写到文件,返回写入的字符数量,换行符算一个数量。

>>> f.write('This is a test\n')15

其他对象需要转换成字符串对象再写入:

>>> value = ('the answer', 42)>>> s = str(value)  # convert the tuple to string>>> f.write(s)18

tell()与seek()

>>> f = open('workfile.txt', 'rb+')>>> f.tell() #需要在二进制模式下使用,返回指针所处字节数。0            #起始位置是0>>> f.write(b'0123456789abcdef')16           #write()返回写入的字节数量>>> f.tell()16           #返回指针位置,指向f#f.seek(offset, from_what) 移动指针,from_what可选0(从头算起,也是默认值),1(当前指针位置算起),2(从尾算起)>>> f.seek(5) #移动指针 指向第5字节5>>> f.read(1)#读取下一字节,即第6字节的内容为b'5'b'5'>>> f.seek(-3, 2) #指向c,差3个字节到达尾部13>>> f.read(1) #读取下一字节,即db'd'
原创粉丝点击