卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34168本站已运行391

Python 循环不适用于 readlines()

python 循环不适用于 readlines()

问题内容

它应该计算“-------------------------”行的数量,但它不起作用,也可以用 print(" test") 不会在控制台中显示,它总是返回 0。但例如行 print("hi") 可以工作。程序就是看不到我的循环,我不知道为什么。 :(

def check_id():
    with open('data.txt', 'r') as f:
        lines = f.readlines()
        ad = 0
                print("hi")  # this line works
        for i in lines:
            print("test")  # this line doesn't work
            if i == "-------------------------":
                ad += 1

        return str(ad)

如果我需要发送完整代码来解决问题,请询问

我将模式“a+”更改为“r”,以便它可以正确读取行,确实如此,但我仍然无法检查数组以获取该行的数量。如果您有任何猜测或解决方案,请写下来。

编辑:这是我的 data.py 和文件 data.txt 中的文本的完整代码

from datetime import date
date = date.today()


def write_note(line):
    with open('data.txt', 'a') as f:
        if line == "!quit":
            f.write('n')
            f.write("-------------------------")
            f.write('n')
            ad = check_id()
            f.write(ad)
            f.write('n')
            f.write("________________________")
            f.write('n')
        else:
            f.write(line)
            f.write("n")


def read_note(id):
    with open('data.txt', 'r') as f:
        pass


def see_all():
    with open('data.txt', 'r') as f:
        get_lines = f.readlines()
        for i in get_lines:
            print(i)
        return get_lines


def del_note(ad):
    with open('data.txt', 'a') as f:
        pass


def logs():
    pass


def check_id():
    with open('data.txt', 'r') as f:
        ad = 0
        for i in f:
            if i == "-------------------------":
                ad += 1

        return str(ad)

现在是 txt 文件:

fugy
hello
hai
bebra

-------------------------
0
________________________
uha
imna
fsjfoe
geso;rsevdn

-------------------------
0  # This one
________________________

我正在尝试制作笔记本,以便可以写笔记并阅读它们。删除 func 我稍后会做。想法是每次添加注释时使这个零更大。


正确答案


我认为问题出在您的 data.txt 文件(可能是空的,因为您提到 "test" 在控制台中不可见,这意味着该脚本不在 for 循环中运行,在其他word: lines 迭代器的长度为零)。

我已经编写了一个工作代码,您可以在下面看到代码和带有脚本输出的测试文件。

代码:

def check_id():
    with open('data.txt', 'r') as opened_file:
        ad = 0
        print("hi")  # this line works
        for i in opened_file:
            print("test")  # this line doesn't work
            if i == "-------------------------":
                ad += 1
        return str(ad)


result = check_id()
print(f"result: {result}")

data.txt的内容:

test_1
-------------------------
test_2
-------------------------
test_3
-------------------------
test_4

测试:

> python3 test.py 
hi
test
test
test
test
test
test
test
result: 0

编辑:

op分享了完整的源代码和使用的data.txt,其中包含cr lf字符(有关该字符的详细信息)。这意味着必须使用 rstrip 方法对这些行进行条纹。

在这种情况下,只有 check_id 函数相关,因此我仅共享修改后的函数:

def check_id():
    with open('data.txt', 'r') as f:
        ad = 0
        for i in f:
            # The cr and lf characters should be removed from line. Please see the above reference for details.
            if i.rstrip() == "-------------------------":
                ad += 1
        return str(ad)


result = check_id()
print(result). # Result is 4
卓越飞翔博客
上一篇: 使用事务和 squirrel 进行 Golang postgresql 查询
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏