【问题标题】:How to Iterate over readlines() in python如何在 python 中迭代 readlines()
【发布时间】:2015-03-09 22:05:05
【问题描述】:

我正在尝试将 txt 文件中的行添加到 python 列表中以进行迭代,并且脚本想要打印每一行并返回错误。我正在使用 readlines() 函数,但是当我使用 list.remove(lines) 时,它会返回错误:File "quotes.py", line 20, in main list.remove(lines) TypeError: remove() takes exactly one argument (0 given).

def main():
while True:
    try:
        text_file = open("childrens-catechism.txt", "r")
        lines = text_file.readlines()
        #    print lines
        #    print len(lines)
        if len(lines) > 0:
            print lines
            list.remove(lines)
            time.sleep(60)
        else:
            print "No more lines!"
        break
        text_file.close()

我看不出我做错了什么。我知道这与 list.remove() 有关。提前谢谢你。

【问题讨论】:

  • 您希望list.remove(lines) 做什么? removelist 类型的方法,因此您必须在特定列表上调用它,而不是在 list 本身上调用它。
  • 我正在尝试打印第一行,将其删除,等待 60 秒,然后打印文件的第二行。我希望它循环直到它到达 txt 文件的末尾。
  • 我认为您应该查看the Python tutorial 并找到一些有关如何在 Python 中处理文件的基本资料。您的代码根本没有遍历文件中的行。此外,您有一个没有任何excepttry,并且您的while True 循环在第一次迭代时将始终break
  • @BrenBarn 这是我第一次接触 Python。我浏览了一些教程,但发现这里对解释问题更有帮助。
  • 如果您将 TypeError 放在标题中并作为单独的问题询问延迟打印行,您的问题可能对未来的读者更有帮助。

标签: python list readlines


【解决方案1】:

你可以这样写。它将为您节省一些时间并提高效率。

import time
def main():
    with open("childrens-catechism.txt", "r") as file:
        for line in file:
            print line,
            time.sleep(60)

【讨论】:

  • 此解决方案无法解决问题:使用readlines() 读取所有行,然后遍历您读取的行。按照上述答案显示的方式进行操作会产生高磁盘 IOPS,并且比一次读取它们要慢。
【解决方案2】:

根据您的要求尝试一下,这将满足您的需求。

import time
def main():
    with open("childrens-catechism.txt", "r") as file:
        for lines in file.readlines():
            if len(lines) > 0:
                for line in lines:
                    print line
                    lines.remove(line)
            else:
                print "No more lines to remove"
            time.sleep(60)

【讨论】:

    【解决方案3】:

    lines 是您的 txt 中的列表。文件,而 list.remove(lines) 不是正确的语法,您试图删除列表中的列表。 list 是 Python 中的一个函数。可以删除lines中的元素like;

    del lines[0]
    del lines[1]
    ...
    

    lines.remove("something")
    

    逻辑是,remove() 正在删除列表中的一个元素,您必须在 remove() 之前写入该列表,然后您必须在 remove() 函数的括号中写入您要删除的内容。

    【讨论】:

      【解决方案4】:

      在打开文件时,我们可以将文件行转换为列表,

      lines = list(open("childrens-catechism.txt", "r"))
      

      我们现在可以从此列表中删除长度大于零的条目,如下所示,

      for line in lines:
          if len(line) > 0:
                  # do sth
                  lines.remove(line)
      

      【讨论】:

        【解决方案5】:

        如果您尝试从文件中读取所有行,然后按顺序打印它们,然后在打印后将其删除,我会推荐这种方法:

            import time
        
            try:
                file = open("childrens-catechism.txt")
                lines = file.readlines()
        
                while len(lines) != 0:
                    print lines[0],
                    lines.remove(lines[0])
                    time.sleep(60)
            except IOError:
                print 'No such file in directory'
        

        这会打印第一行,然后将其删除。当第一个值被删除时,列表向上移动,使上一行 (lines[1]) 成为列表的新起点,即lines[0]

        已编辑:

        如果您想从文件以及行列表中删除该行,您必须这样做:

            import time
        
            try:
                file = open("childrens-catechism.txt", 'r+')  #open the file for reading and writing
                lines = file.readlines()
        
                while len(lines) != 0:
                    print lines[0],
                    lines.remove(lines[0])
                    time.sleep(60)
        
                file.truncate(0) #this truncates the file to 0 bytes
            except IOError:
                print 'No such file in directory'
        

        至于从文件行中删除行,我不太确定这是否可行或有效。

        【讨论】:

        • 是否从文件中删除该行?
        • @joshlsullivan 最上面的代码只删除行列表中的行,而不是文件中的行。编辑后的版本将文件截断为 0 字节;基本上删除了里面的所有文字。
        猜你喜欢
        • 2011-04-02
        • 1970-01-01
        • 1970-01-01
        • 2013-11-03
        • 2015-11-23
        • 2020-09-29
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多