python 文件读取

  • 读取整个文件
    操作文件的第一步就是得打开要操作的文件,然后进行读取。在python中我们可以使用open函数来打开一个文件,然后使用read方法来读取文件。

示例:

import os

with open('data.txt', encoding='utf-8') as file_obj:
    contents = file_obj.read()
    print(contents.rstrip())

首先,我们看下open函数的定义:

def open(file: Union[str, bytes, int], mode: str = ..., buffering: int = ..., encoding: Optional[str] = ...,              errors: Optional[str] = ..., newline: Optional[str] = ..., closefd: bool = ...) Inferred type: (file: Union[str, bytes, int], mode: str, buffering: int, encoding: Optional[str], errors: Optional[str], newline: Optional[str], closefd: bool) -> IO Open file and return a stream. Raise IOError upon failure. 

在一般情况下,我们打开文件只需要传入文件路径即可,这里我们读取的文件的内容是中文,为了避免乱码,我们在这里指定了编码格式。其他的参数等我们用到的时候,在仔细说明。

open函数返回一个表示文件的对象,python会将这个对象存储在我们的变量file_obj中,这样我们就可以方便的操作了。

data.txt和我们的文件在同一目录下,所以直接使用名称即可:
这里写图片描述

如果是其他路径(非程序目录),那么直接使用绝对路径:
我是在window的开发环境,以windows为例:

file_path = 'D:\data.txt'
with open(file_path, encoding='utf-8') as file_obj:

read()方法读取文件的整个内容,并将其作为字符串返回。

其他的我们先不做说明,先来看下文件运行结果,是否将文件内容成功读取:
这里写图片描述

虽然我们的文本有点多,大家可以仔细看一下,就当回味一下高中生活了。

有些仔细一点的朋友可能发现了,这里我们只是使用了open和read来打开和读取文本文件,但是并没有关闭文件句柄,这一点有点奇怪,我们在其他的语言open和close总是成双成对的出现的。

这里的奥妙就在于with关键字,使用该关键字可以让python去确定,我们在不需要访问文件的时候自动关闭文件。也就是说我们只需要打开和操纵文件,python会在合适的机会关闭文件。

我们在将文件打开后,打印一下这个file_obj到底有些什么:

 print(dir(file_obj))

输出:

['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '_
_init_subclass__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable',
 '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline',
'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines']

这些都是和文件操作相关的,在这里不会给大家做仔细说明,只是让大家对这些东西有个印象,等遇到了,我们在说怎么使用。

  • 逐行读取
    上面是读取整个文件内容,接下来我们看下怎么逐行读取文件,为了方便演示,我们将data的内容修改为几行数字。

在逐行读取文本的时候,常见的可以使用for循环来读取:

with open(file_path, encoding='utf-8') as file_obj:
    for line in file_obj:
        print(line)

还有就是使用readline方法来逐行读取文件:

with open(file_path, encoding='utf-8') as file_obj:
    line = file_obj.readline()
    while line != '':
        print(line)
        line = file_obj.readline()

另外一种倒像是第二种的升级版,它会一次性的将文件逐行读取存入一个列表中,一遍我们使用:

with open(file_path, encoding='utf-8') as file_obj:
    lines = file_obj.readlines()

for line in lines:
    print(line)

lines 在代码块with之外也是可以使用的。

说了几种常见的使用方法,我们来看下程序运行输出:

D:\Programs\python\HelloWorld\venv\Scripts\python.exe D:\Programs\python\HelloWorld\python_file.py
1234

2345

3456

4567


Process finished with exit code 0

大家可以看到,我们读取的数据没有任何问题,但是每行数据之间都会存在多余的空行,这是因为在文件中每行的末尾都会有一个看不见的换行符。如果我们希望去除这些多余空行,可以使用rstrip()函数:

def rstrip(self, chars: Optional[str] = ...) Inferred type: (self: str, chars: Optional[str]) -> str 
S.rstrip([chars]) -> str
Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead.

那么上面的例子我们该这么修改:

with open(file_path, encoding='utf-8') as file_obj:
    lines = file_obj.readlines()

for line in lines:
    print(line.rstrip())

输出:

1234
2345
3456
4567
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章