Python 如何将文件逐行读取到列表中?

Python 如何将文件逐行读取到列表中?,python,string,file,readlines,Python,String,File,Readlines,如何在Python中读取文件的每一行,并将每一行作为元素存储在列表中 我想逐行读取文件,并将每一行附加到列表的末尾。这将从文件中生成一个“数组”行 lines = tuple(open(filename, 'r')) open返回一个可以迭代的文件。当您迭代一个文件时,您会从该文件中获得行tuple可以获取迭代器,并从给定的迭代器中为您实例化一个tuple实例行是从文件的行创建的元组。这比必要的更明确,但做您想要的 lines = tuple(open(filename, 'r')) 打开(

如何在Python中读取文件的每一行,并将每一行作为元素存储在列表中

我想逐行读取文件,并将每一行附加到列表的末尾。

这将从文件中生成一个“数组”行

lines = tuple(open(filename, 'r'))

open
返回一个可以迭代的文件。当您迭代一个文件时,您会从该文件中获得行
tuple
可以获取迭代器,并从给定的迭代器中为您实例化一个tuple实例<代码>行是从文件的行创建的元组。

这比必要的更明确,但做您想要的

lines = tuple(open(filename, 'r'))
打开(“file.txt”)作为文件
行=[]
对于文件中的行,请参见:
行。追加(行)
请参见:

打开('filename')作为f的
:
行=f.读行()
或使用换行符:

打开('filename')作为f的
:
lines=[line.rstrip()表示f中的行]
另一个选项是,例如:

import numpy as np
data = np.genfromtxt("yourfile.dat",delimiter="\n")
def process(line):
    if 'save the world' in line.lower():
         superman.save_the_world()
open('afile')   # opens the file named afile in the current working directory
open('adir/afile')            # relative path (relative to the current working directory)
open('C:/users/aname/afile')  # absolute path (windows)
open('/usr/local/afile')      # absolute path (linux)

这将使
数据
成为一个NumPy数组,其行数与文件中的行数相同。

如果您想从命令行或stdin读取文件,也可以使用
文件输入
模块:

# reader.py
import fileinput

content = []
for line in fileinput.input():
    content.append(line.strip())

fileinput.close()
按如下方式向其传递文件:

$ python reader.py textfile.txt 
请在此处阅读更多信息:

现在variable out是您想要的列表(数组)。您可以选择:

for line in out:
    print (line)
或:


您将得到相同的结果。

如果您希望包含
\n

with open(fname) as f:
    content = f.readlines()
with open(fname) as f:
    content = f.read().splitlines()
如果不希望包含
\n

with open(fname) as f:
    content = f.readlines()
with open(fname) as f:
    content = f.read().splitlines()

在文件上使用列表理解还有一个选项

lines = [line.rstrip() for line in open('file.txt')]

这应该是一种更有效的方法,因为大部分工作都是在Python解释器中完成的。

将文件的行读入列表的干净而简洁的方法


首先也是最重要的一点是,您应该专注于以一种高效的方式打开文件并读取其内容。以下是我个人不喜欢的一种方式:

infle=open('my_file.txt','r')#打开文件进行读取。
data=infle.read()#读取文件的内容。
infle.close()#关闭文件,因为我们已经使用完它。
相反,我更喜欢下面的方法,即打开文件进行读写 非常干净,不需要关闭文件的额外步骤 一旦你用完它。在下面的声明中,我们正在打开该文件 用于读取并将其分配给变量'infle'。一旦代码在 此语句已完成运行,文件将自动关闭

#打开文件进行读取。
打开('my_file.txt','r')作为填充:
data=infle.read()#将文件内容读入内存。
现在我们需要把重点放在将这些数据放入Python列表中,因为它们是可移植的、高效的和灵活的。在您的例子中,理想的目标是将文本文件的每一行放入一个单独的元素中。为了实现这一点,我们将使用splitlines()方法,如下所示:

from pathlib import Path
p = Path('my_text_file')
lines = p.read_text().splitlines()
#返回行列表,在行边界处断开。
my_list=data.splitlines()

最终产品:

line 1
line 2
line 3
x = []
with open("myfile.txt") as file:
    for l in file:
        x.append(l.strip())
>>> x = open("myfile.txt").read().splitlines()
>>> x
['line 1', 'line 2', 'line 3']
>>> x = open("myfile.txt").readlines()
>>> x
['linea 1\n', 'line 2\n', 'line 3\n']
def print_output(lines_in_textfile):
    print("lines_in_textfile =", lines_in_textfile)

y = [x.rstrip() for x in open("001.txt")]
print_output(y)

with open('001.txt', 'r', encoding='utf-8') as file:
    file = file.read().splitlines()
    print_output(file)

with open('001.txt', 'r', encoding='utf-8') as file:
    file = [x.rstrip("\n") for x in file]
    print_output(file)
#打开文件进行读取。
打开('my_file.txt','r')作为填充:
data=infle.read()#将文件内容读入内存。
#返回行列表,在行边界处断开。
my_list=data.splitlines()
测试我们的代码:

  • 文本文件的内容:
A fost odatãca-n povesti,
一个福斯特·卡尼奥达特,
在粗糙的穆斯林世界,
O prea frumoasãfatã。
  • 出于测试目的打印报表:
打印我的列表#打印列表。
#打印列表中的每一行。
对于my_列表中的行:
打印行
#打印此列表中的第四个元素。
打印我的_列表[3]
  • 输出(因unicode字符而外观不同):
['A fost odat\xc3\xa3 ca-n povesti','A fost ca-niciodat\xc3\xa3',
“Din rude m\xc3\xa3ri\xc3\xaemp\xc3\xa3r\xc3\xa3testi,”,O prea
frumoas\xc3\xa3 fat\xc3\xa3']
这是一个古老的节日,这是一个古老的节日
împãrãtesti,O prea frumoasãfatã。
O prea frumoasãfatã。

最简单的方法

一个简单的方法是:

  • 将整个文件作为字符串读取
  • 将字符串逐行拆分
  • 在一行中,这将给出:

    lines = open('C:/path/file.txt').read().splitlines()
    
    然而,这是一种非常低效的方法,因为这将在内存中存储2个版本的内容(对于小文件来说可能不是什么大问题,但仍然如此)。[谢谢马克·艾默里]

    有两种更简单的方法:

  • 将文件用作迭代器
  • 如果您使用的是Python 3.4或更高版本,最好使用
    pathlib
    为您的文件创建一个可用于程序中其他操作的路径:
  • 根据Python的说法,将文本文件转换为
    列表的最简单方法是:

    with open('file.txt') as f:
        my_list = list(f)
        # my_list = [x.rstrip() for x in f] # remove line breaks
    

    如果只需要迭代文本文件行,可以使用:

    with open('file.txt') as f:
        for line in f:
           ...
    

    旧答案:

    readlines()一起使用

    如果您不关心关闭文件,则此一行程序可以工作:

    lines = open('file.txt').readlines()
    
    传统方式:


    您还可以在NumPy中使用loadtxt命令。这比genfromtxt检查的条件更少,因此可能更快

    import numpy
    data = numpy.loadtxt(filename, delimiter="\n")
    

    您可以按照建议简单地执行以下操作:

    with open('/your/path/file') as f:
        my_lines = f.readlines()
    
    请注意,这种方法有两个缺点:

    1) 您将所有行存储在内存中。在一般情况下,这是一个非常糟糕的主意。该文件可能非常大,并且可能会耗尽内存。即使它不是很大,也只是浪费内存

    2) 这不允许在读取每一行时对其进行处理。因此,如果在此之后处理您的行,则效率不高(需要两次而不是一次)

    对于一般情况,更好的方法是
    line 1
    line 2
    line 3
    
    x = []
    with open("myfile.txt") as file:
        for l in file:
            x.append(l.strip())
    
    >>> x = open("myfile.txt").read().splitlines()
    >>> x
    ['line 1', 'line 2', 'line 3']
    
    >>> x = open("myfile.txt").readlines()
    >>> x
    ['linea 1\n', 'line 2\n', 'line 3\n']
    
    def print_output(lines_in_textfile):
        print("lines_in_textfile =", lines_in_textfile)
    
    y = [x.rstrip() for x in open("001.txt")]
    print_output(y)
    
    with open('001.txt', 'r', encoding='utf-8') as file:
        file = file.read().splitlines()
        print_output(file)
    
    with open('001.txt', 'r', encoding='utf-8') as file:
        file = [x.rstrip("\n") for x in file]
        print_output(file)
    
    lines_in_textfile = ['line 1', 'line 2', 'line 3']
    lines_in_textfile = ['line 1', 'line 2', 'line 3']
    lines_in_textfile = ['line 1', 'line 2', 'line 3']
    
    #!/bin/python3
    import os
    import sys
    abspath = os.path.abspath(__file__)
    dname = os.path.dirname(abspath)
    filename = dname + sys.argv[1]
    arr = open(filename).read().split("\n") 
    print(arr)
    
    python3 somefile.py input_file_name.txt
    
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    # Define data
    lines = ['     A first string  ',
             'A Unicode sample: €',
             'German: äöüß']
    
    # Write text file
    with open('file.txt', 'w') as fp:
        fp.write('\n'.join(lines))
    
    # Read text file
    with open('file.txt', 'r') as fp:
        read_lines = fp.readlines()
        read_lines = [line.rstrip('\n') for line in read_lines]
    
    print(lines == read_lines)
    
    lst = list(open(filename))
    
    open('afile')   # opens the file named afile in the current working directory
    open('adir/afile')            # relative path (relative to the current working directory)
    open('C:/users/aname/afile')  # absolute path (windows)
    open('/usr/local/afile')      # absolute path (linux)
    
    open(filename)
    open(filename, 'r')
    
    open(filename, 'rb')
    
    f = open(filename)
    # ... do stuff with f
    f.close()
    
    f = open(filename)
    # nothing in between!
    try:
        # do stuff with f
    finally:
        f.close()
    
    with open(filename) as f:
        # do stuff with f
    # The file is always closed after the with-scope ends.
    
    with open(filename) as f:
        for line in f:
            print(line)
    
    with open(filename) as f:
        for line in f:
            print(line[:-1])
    
    with open(filename) as f:
        for line in f:
            if line.endswith('\n'):
                line = line[:-1]
            print(line)
    
    with open(filename) as f:
        for line in f:
            print(f.rstrip())
    
    with open(filename) as f:
        lst = list(f)
    
    with open(filename) as f:
        lst = [line.rstrip() for line in f]
    
    with open(filename) as f:
        lst = f.readlines()
    
    with open(filename) as f:
        lst = f.read().split('\n')
    
    with open(filename) as f:
        lst = f.read().splitlines()
    
    contents = []
    for line in open(filepath, 'r').readlines():
        contents.append(line.strip())
    
    contents = [line.strip() for line in open(filepath, 'r').readlines()]
    
    from pathlib import Path
    p = Path('my_text_file')
    lines = p.read_text().splitlines()
    
    from pathlib import Path
    Path('filename').write_text('foo\nbar\nbaz')
    
    filename = 'filename'
    with open(filename) as f:
        for line in f:
            line # do something with the line
    
    import fileinput
    
    for line in fileinput.input(filename): 
        line # process the line
    
    for line in fileinput.input([filename]*2): 
        line # process the line
    
    list(fileinput.input(filename))
    
    filename = 'filename'
    
    with open(filename) as f:
        f.readlines()
    
    from pathlib import Path
    
    path = Path(filename)
    
    with path.open() as f:
        f.readlines()
    
    with path.open() as f:
        list(f)
    
    path.read_text().splitlines()
    
    path.read_text().splitlines(keepends=True)
    
    line_list = []
    for line in fileinput.input(filename):
        line_list.append(line)
    
    line_list
    
    line_list = []
    line_list.extend(fileinput.input(filename))
    line_list
    
    [line for line in fileinput.input(filename)]
    
    list(fileinput.input(filename))
    
    fpath = 'dummy.txt'
    with open(fpath, "r") as f: lst = [line.rstrip('\n \t') for line in f]
    
    print lst
    >>>['THIS IS LINE1.', 'THIS IS LINE2.', 'THIS IS LINE3.', 'THIS IS LINE4.']
    
    import csv
    fpath = 'dummy.txt'
    with open(fpath) as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='   ')
        lst = [row[0] for row in csv_reader] 
    
    print lst
    >>>['THIS IS LINE1.', 'THIS IS LINE2.', 'THIS IS LINE3.', 'THIS IS LINE4.']
    
    with open(myFile, "r") as f:
        excludeFileContent = list(filter(None, f.read().splitlines()))
    
    lines = list(open('filename'))
    
    lines = tuple(open('filename'))
    
    lines = set(open('filename'))
    
    with open('filename') as f: lines = list(f) 
    
    import os
    
    # handle files using a callback method, prevents repetition
    def _FileIO__file_handler(file_path, mode, callback = lambda f: None):
      f = open(file_path, mode)
      try:
        return callback(f)
      except Exception as e:
        raise IOError("Failed to %s file" % ["write to", "read from"][mode.lower() in "r rb r+".split(" ")])
      finally:
        f.close()
    
    
    class FileIO:
      # return the contents of a file
      def read(file_path, mode = "r"):
        return __file_handler(file_path, mode, lambda rf: rf.read())
    
      # get the lines of a file
      def lines(file_path, mode = "r", filter_fn = lambda line: len(line) > 0):
        return [line for line in FileIO.read(file_path, mode).strip().split("\n") if filter_fn(line)]
    
      # create or update a file (NOTE: can also be used to replace a file's original content)
      def write(file_path, new_content, mode = "w"):
        return __file_handler(file_path, mode, lambda wf: wf.write(new_content))
    
      # delete a file (if it exists)
      def delete(file_path):
        return os.remove() if os.path.isfile(file_path) else None
    
    file_ext_lines = FileIO.lines("./path/to/file.ext"):
    for i, line in enumerate(file_ext_lines):
      print("Line {}: {}".format(i + 1, line))