栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

#python学习笔记(八)#文件的打开,读写和查找

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

#python学习笔记(八)#文件的打开,读写和查找

目录

1 Open files

2 Reading files

3 Searching through a file

4 Letting the user choose the file name

5 Writing files


1 Open files

If the open is successful, the operating system returns us a file handle. The file
handle is not the actual data contained in the file, but instead it is a “handle” that
we can use to read the data. 

open函数返回的不是文件的实际内容,而是我们可以用来操作(打开,关闭,读写)文件的一个handle

>>> fhand = open('mbox.txt')   ###open('文件名')
>>> print(fhand)
<_io.TextIOWrapper name='mbox.txt' mode='r' encoding='cp1252'>   ###并非文件的实际内容


>>> fhand = open('stuff.txt')
Traceback (most recent call last):
File "", line 1, in 
FileNotFoundError: [Errno 2] No such file or directory: 'stuff.txt'   ###文件不存在时会报错

2 Reading files

When the file is read using a for loop in this manner, Python takes care of splitting
the data in the file into separate lines using the newline character. Python reads
each line through the newline and includes the newline as the last character in the
line variable for each iteration of the for loop

用for循环逐行读取文件,python根据换行符进行分隔

fhand = open('mbox-short.txt')  ##建立handle
count = 0
for line in fhand:              ##逐行读取
    count = count + 1           ##每读一行count+1
print('Line Count:', count)

If you know the file is relatively small compared to the size of your main memory,
you can read the whole file into one string using the read method on the file handle.

当文件比较小的时候,可以用file数据的read方法读取,此时返回包括所有字符的字符串

>>> fhand = open('mbox-short.txt')  ###建立连接
>>> inp = fhand.read()      ###inp为file中的所有字符
>>> print(len(inp))
94626                       ###总共包含94626个字符
>>> print(inp[:20])
From stephen.marquar        ###前20个字符

3 Searching through a file

▲示例:查找以某字符开头的行

fhand = open('mbox-short.txt')  ###建立handle
for line in fhand:              ###循环读取数据
    if line.startswith('From:'):  ###判断每行是否以From:开头,用到字符串的方法startswith
        print(line)               ###判断为真时输出语句

运行结果: 

From: stephen.marquard@uct.ac.za

From: louis@media.berkeley.edu

From: zqian@umich.edu

From: rjlowe@iupui.edu
...

每行之间存在一个空行,因为每行的结尾有一个newline(n) 字符(python根据这个来识别不同行),而 print语句会在输出时再加上一个newline字符,从而导致出现空行

解决方法:

fhand = open('mbox-short.txt')
for line in fhand:
    line = line.rstrip()        ###string的函数rstrip删除每行最右边的newline character
    if line.startswith('From:'):
        print(line)

运行结果:

 From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
From: cwen@iupui.edu
...

▲示例:查找包含某字段的行

fhand = open('mbox-short.txt')    ###建立handle
for line in fhand:                ###循环读取
    line = line.rstrip()          ###删newline
    if line.find('@uct.ac.za') == -1: continue   
###string方法find判断该行是否包含@uct.ac.za, 未找到返回-1,继续下一次迭代
    print(line)   ###找到则输出该行

4 Letting the user choose the file name

▲用户输入文件名,try-except

fname = input('Enter the file name: ')  ###用户输入文件名
try:
    fhand = open(fname)
except:
    print('File cannot be opened:', fname)
    exit()                                        ###try-except

count = 0
for line in fhand:
    if line.startswith('Subject:'):
    count = count + 1                             ###查找Subject:开偷我的行并计数

print('There were', count, 'subject lines in', fname)  ###输出结果

5 Writing files

▲打开方式为‘w',open函数的第二个参数

>>> fout = open('output.txt', 'w')
>>> print(fout)
<_io.TextIOWrapper name='output.txt' mode='w' encoding='cp1252'>

▲利用file对象的write方法写入数据,参数为写入的内容,返回写入数据的字符数

>>> line1 = "This here's the wattle,n"
>>> fout.write(line1)
24

▲写入完成后关闭文件

>>> fout.close()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/350734.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号