简体   繁体   English

如何在python中迭代readlines()

[英]How to Iterate over readlines() in python

I am trying to add lines from a txt file to a python list for iteration, and the script wants to print every line and return an error. 我试图从txt文件添加行到python列表进行迭代,脚本想要打印每一行并返回错误。 I'm using the readlines() function, but when I use list.remove(lines), it returns an error: File "quotes.py", line 20, in main list.remove(lines) TypeError: remove() takes exactly one argument (0 given). 我正在使用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()

I can't see what I'm doing wrong. 我看不出我做错了什么。 I know it has to do with list.remove(). 我知道它与list.remove()有关。 Thank you in advance. 先感谢您。

You can write in this way. 你可以这样写。 It will save you some time and give you more efficiency. 它会为您节省一些时间并提高效率。

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

lines is a list here from your txt. lines是你的txt的列表。 files, and list.remove(lines) is not a correct syntax, you trying to delete a list on list. 您尝试删除列表中的列表时,文件和list.remove(lines)语法不正确。 list is a function in Python. list是Python中的一个函数。 You can delete the elements in lines like; 您可以删除的元素lines等;

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

or 要么

lines.remove("something")

The logic is, remove() is deleting an element in a list, you have to write that list before remove() after then you have to write the thing that you want to delete in paranthesis of remove() function. 逻辑是, remove()正在删除列表中的元素,你必须在remove()之前编写该列表,然后你必须在remove()函数的paranthesis中编写你要删除的东西。

Try this as per your requirements, this will do what you need. 根据您的要求尝试这个,这将满足您的需求。

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)

On opening a file, we can convert the file lines onto a list, 在打开文件时,我们可以将文件行转换为列表,

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

From this list we can now remove entries with length greater than zero, like this, 从这个列表中我们现在可以删除长度大于零的条目,像这样,

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

If you are trying to read all the lines from the file and then print them in order, and then delete them after printing them I would recommend this approach: 如果您尝试从文件中读取所有行,然后按顺序打印它们,然后在打印后删除它们,我会建议这种方法:

    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'

This prints the first line and then deletes it. 这将打印第一行,然后将其删除。 When the first value is removed, the list shifts one up making the previous line ( lines[1] ) the new start to the list namely lines[0] . 当第一个值被移除时,列表向上移动一个,使前一行( lines[1] )新的开始到列表,即lines[0]

EDITED: 编辑:

If you wanted to delete the line from the file as well as from the list of lines you will have to do this: 如果要删除文件中的行以及行列表中的行,则必须执行以下操作:

    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'

As far as deleting the lines from the file line for line I am not too sure if that is possible or efficient. 至于从行I的文件行中删除行,我不太确定是否可行或有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM