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
,其中包含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
以上是Python 循环不适用于 readlines()的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...
