将文件数据转换为列表

2023-09-22 05:34:40

在这里,您将学习获取文件数据并将其转换为列表的替代方法。

Python 有一个内置的 open() 函数,它返回一个类似文件的对象,充当迭代器。 我们还可以使用os模块中的fdopen()方法来读取文件。然后还有可用于此目的的fileinput模块。

Using File object

使用内置open() function从文件流中读取三种方法——read()readline()readlines()

出于演示目的,我们有一个zen.txt文件,其中包含Zen of Python的前几行:

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.

以下代码片段将此文件读入列表。readline()方法末尾包含一个换行符,该换行符由string对象的strip()方法删除。

fileobj=open("zen.txt")
lines=[]
for line in fileobj:
    lines.append(line.strip())
print(lines)

输出:

['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']

readlines() 方法很容易返回列表,而无需执行迭代。

fileobj=open("zen.txt")
lines=fileobj.readlines()
lines=[line.strip() for line in lines]
print(lines)

输出:

['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']

我们还可以使用 read() 方法并使用 split() 方法在每个换行符处拆分字符串。

fileobj=open("zen.txt")
lines=fileobj.read().split('n')
print(lines)

输出:

['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']

继续File对象,我们可以从中获取一个迭代器,并在遍历它时构造一个列表。

fileobj=open("zen.txt")
it=iter(fileobj)
lines=[]
while True:
    try:
        line=next(it)
        line=line.strip()
        lines.append(line)
    except StopIteration:
        break
print(lines)

输出:

['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']

使用操作系统模块转换为列表

os模块的大部分功能都取决于操作系统,它还具有许多用于文件处理的工具。 在这里,我们使用 fdopen() 函数,它本质上将file descriptor包装在文件对象中。文件描述符由 os.open() 函数获取,如下所示:

import os
fd=os.open("zen.txt", os.O_RDONLY)
fileobj=os.fdopen(fd)
lines=fileobj.readlines()
lines=[line.strip() for line in lines]
print(lines)

输出:

['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']

使用文件输入模块转换为列表

尽管该模块的真正功能是它有助于迭代作为命令行参数提供的多个文件,但我们将使用其input()函数打开我们的 zen.txt并在列表中读取它。

import fileinput
lines=[]
for line in fileinput.input(files=['zen.txt']):
    lines.append(line.strip())
print(lines)

输出:

['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.', 'Sparse is better than dense.']

正如我们所看到的,在逐行读取列表中的文件时,有很多选项可供选择。