Python 基础系列 12 - 文件读写 输入输出格式化 File I/O and Formatting

引言

今天主要梳理一下 Python 中有关 File 的基本操作,通过实例来验证有关 File 各方法的功能,对文件操作的各种模式,异常处理,最后还涉及到有关 format() 的基本应用。更多 Python 基础系列文章,请参考 Python 基础系列大纲

内容提要:

  • 文件输入输出操作 File IO
    文件操作函数
    文件操作模式
    文件编码
    文件异常处理
  • 字符串格式化

文件输入输出操作 File IO

File IO 函数

FunctionInterpretationExample
open()opens file accessf=open(“myfile.txt”)
close()closes file accessf.close()
read()reads the entire content of the file into a stringf.read()
readline()reads one line from a file, advances iterator to the nextf.readline()
readlines()reads all lines from a file into a list with each line as a list itef.readlines()
write()writes data to a filef.write(data)
writelines()writes a list of strings to a file, each item on a separate linef.writelines(list_of_lines)
with open()creates a context manager for a filewith open(“myfile”) as f: do something with f

open() function

open(name[, mode[, encoding, buffering]])

参数描述
name文件路径
mode操作文件模式
encoding编码方式
buffering寄存状态,buffering设为0,访问不寄存,为1访问文件寄存,如果为大于1的整数,表明寄存区的缓冲大小

编码格式:
Windows 一般默认 txt 为 ANSI 编码格式,出现乱码一般都是原文件的编码方式和打开指定的编码不一致所致。可以用国际兼容的编码方式 UTF-8,中文也支持。

文件操作模式:

操作模式描述
r只读模式打开文件,文件的指针在文件开头,即从第一行第一列读取文件内容
rb在二进制格式下以只读模式打开文件,,文件指针放在开头,即从第一行第一列读取文件内容
r+读写模式打开文件,文件指针在开头
rb+在二进制格式下以读写模式打开文件,文件指针在文件开头
w以写模式打开文件,文件指针在文件开头,即从文件头开始编辑,原有内容被删除,如果文件不存在,会自动创建文件
wb在二进制格式下以写模式打开文件,如果文件存在,从头开始编辑,原有内容被删除,文件不存在,自动创建文件
w+读写模式打开文件,文件存在,从头开始编辑,原有内容被删除,文件不存在,自动创建文件
wb+在二进制格式下以读写模式打开文件,从头开始编辑,原有内容被删除,文件不存在则自动创建文件
a打开文件追加内容,如果文件存在,文件指针放在文件结尾,即继续先前的文件继续编辑,文件不存在,自动创建文件
ab在二进制格式下追加文件内容,文件指针放在文件结尾,即继续先前的文件继续编辑,文件不存在,自动创建文件
a+以读写模式追加文件内容,文件指针在文件结尾,即继续先前的文件继续编辑,文件不存在,自动创建文件
ab+在二进制格式下追加文件内容,文件指针在文件结尾,即继续先前的文件继续编辑,文件不存在,自动创建文件

注意:后面有带 b 的方式,不需要考虑编码方式。有带 + 号的,则可读可写,不过它们之间还是有区别的

下面通过几个典型的例子说明各操作模式的差别:
text.txt content:
line1
line2
last line

r 模式:原样输出

f = open(r'c:\Users\xiakx\Desktop\test.txt', 'r')
print(f.read())
# output:
line1
line2
last line

rb 模式:一行输出,但是有字节格式b开头和换行标识符

f = open(r'c:\Users\xiakx\Desktop\test.txt', 'rb')
print(f.read())
#output:
b'line1\r\nline2\r\nlast line'

w 模式:覆盖原来的内容

f = open(r'c:\Users\xiakx\Desktop\test.txt', 'w')
f.write('new line')
f.close()
# result: 
new line

a 模式:紧接着最后一个字符追加写入新的内容

f = open(r'c:\Users\xiakx\Desktop\test.txt', 'a')
f.write('new line')
f.close()
# result:
line1
line2
last linenew line

r+ 模式:有点出乎意料了,其实文件内空已经变成了
new lineine2
last line
但是 read 输出只是一部分呢。这是因为光标的变化引起的,进行 write 的时候,光标在开头,会覆盖原有的内容,写完光标会停在写完的最后一个字符上,接着就 read 操作,所以光标前面的字符不会读到。

f = open(r'c:\Users\xiakx\Desktop\test.txt', 'r+')
f.write('new line')
print(f.read())
f.close()
#output:
ine2
last line

w+ 模式:写之前,写之后,read 输出都为空了。其实最后文件内容是:
new line
只能说明 open 时就已经覆盖了原来的内容,后面写入新内容,但是此时光标是停在最后一个字符后面的,接着进行读,光标后面就不有内容了。

f = open(r'c:\Users\xiakx\Desktop\test.txt', 'w+')
print(f'read: {f.read()}')
f.write('new line')
print(f'read: {f.read()}')
f.close()
# output:
read: 
read: 

wb 模式:原来的文件内容被覆盖了,新内容会根据换行符换行,所以现在文件内容是:
new line1
new line2

f = open(r'c:\Users\xiakx\Desktop\test.txt', 'wb')
f.write(b'new line1\nnew line2')
f.close()

下面是举例看看各 function
ANSI 编码格式 test.txt 内容:
line1
line2
第三行
last line

readlines() reads all lines in the file,puts them in a list
and keeps new line character \n

f = open(r'c:\Users\xiakx\Desktop\test.txt', encoding = 'ansi')
list_lines = f.readlines()
print(f'readlines():{list_lines}')
for line in list_lines:
      print(line)
f.close()

数组里的每个 item 的 \n 换行符也输出了

# output:
readlines():['line1\n', 'line2\n', '第三行\n', 'last line']
line1

line2

第三行

last line

readline():一行接着一行读,而且每行都会带 \n 换行符
另外 Reads n characters for the line (readline(n))

f = open(r'c:\Users\xiakx\Desktop\test.txt', encoding = 'ansi')
line1 = f.readline()
line2 = f.readline()
line3 = f.readline()
line4 = f.readline()
print(line1)
print(line2)
print(line3)
print(line4)
f.close()
#output:
line1

line2

第三行

last line

writelines(): Writing ALL lines to a file
一个新的文件 test_copy.txt 生成,而且内容和 text.txt 是一样的:
line1
line2
第三行
last line

f = open(r'c:\Users\xiakx\Desktop\test.txt', encoding = 'ansi')
readlines = f.readlines()
f = open(r'c:\Users\xiakx\Desktop\test_copy.txt', 'w') 
f.writelines(readlines)
f.close()

File I/O exception handling

如果文件存在将按行逐行输出,否则抛出异常。

try:
    f = open('test.txt', encoding = 'ansi')
    for data in f:
        print(data)
except IOError as e:
    print(e)
finally:
    if f:
        f.close()
# output:
[Errno 2] No such file or directory: 'test.txt'

还有一种嵌套异常处理, 文件不存在,open()失败,也就不用 call close(), 因为文件是 None,但是如果读的时候出现异常,不管怎样一定要执行close()

try:
    f = open('test.txt', encoding = 'ansi')
    try:
        for data in f:
            print(data)
    finally:
        f.close()
except IOError as e:
    print(e)

with open():以上操作我们都要 f.close(),我们读取文件是把文科读取到内存中的,如果没关闭它,它就会一直占用系统资源,而且还可能导致其他不安全隐患。还有一种方法让我们不用特意关注关闭文件。那就是 with open(),无论在文件使用中遇到什么问题都能安全的退出,即使发生错误,退出运行时环境时也能安全退出文件并给出报错信息。

try:
    with open(r'c:\Users\xiakx\Desktop\test.txt', encoding = 'ansi') as fin,\
    open(r'c:\Users\xiakx\Desktop\test_copy.txt', 'w') as fout:
        for line in fin:
            fout.write(line)
except IOError as e:
    print(e)

字符串格式化

Strings: format():

# positional arguments 
my_str = 'I am {0} student of Python {1} course.'
new_str = my_str.format('John Brown', 200)
# keyword arguments 
my_str2 = 'I am {name} student of Python {level} course.'
new_str2 = my_str2.format(name='John Brown', level=200)
print ('new_str: ' + new_str) 
print ('new_str2: ' +new_str2)
# output:
new_str: I am John Brown student of Python 200 course.
new_str2: I am John Brown student of Python 200 course.

字符截断:
.n—>截取字符个数
:n—>宽度,不足空格填充

s1 = 'Timothy'
# truncating strings to 3 letters
print('{:.3}'.format(s1))
# truncating strings to 3 letters and padding
print('{:>5.3}'.format(s1))
# truncating strings to 3 letters, central aligment and padding
print('{:^5.3}'.format(s1))
# output:
Tim
  Tim
 Tim

Output Formatting: format()
d: integer
f: float
s: string
:n---->n个宽度
:0n—>不足n个宽度用0补齐
.nf---->n位小数

# Number formating 
a = 53 
b = 4.897 
print ("{:4d}".format(a)) 
print ("{:04d}".format(a)) 
print ("{:4.2f}".format(b)) 
print ("{:06.2f}".format(b))
# output:
  53
0053
4.90
004.90

<: left align
>: right align
=: padding between the sign and number 符号和数字之单填空格
^: centered

# Number formating 
a = 53 
b = 4.897 
# Alignement 
print ("{:^4d}".format(a)) 
print ("{:>5.2f}".format(b)) 
print ("{:<5.2f}".format(b)) 
print ("{:=7.2f}".format(-b))
# output:
 53 
 4.90
4.90 
-  4.90
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值