python - 读取文本文件时未检测到某些行?
问题描述
text_file.txt
我得到了第一个打印语句的输出,但没有第二个打印语句的输出。请给我正确的代码,我需要编码或解码吗?请帮助我,我是 python3 的新手
解决方案
这是您要实现的目标的更直接的实现。您可以将文件读入 Python 列表并通过 Python 列表索引引用每一行
with open('text_file.txt','r') as f: # automatically closes the file
input_file = f.readlines() # Read all lines into a Python list
for line_num in range(len(input_file)):
if "INBOIS BERCUKAI" in input_file[line_num]:
print(input_file[line_num + 2]) # offset by any number you want
# same for other if statements
推荐阅读
- angular - 背景图片网址在我的角度项目中不起作用
- html - 使用 HTTP post 方法而不是 DELETE 是否有任何危害
- c# - 更新 UnitAmount of Stripe 结帐价格
- javascript - office-ui-fabric / fluent-ui Grouped DetailsList
- docker - 如何使用 H2 数据库容器化一个简单的 Java 应用程序
- vue.js - 防止vue cli删除dist中的所有文件
- c++ - 你必须在 OpenGL 程序中调用 glfwSwapInterval(1) 吗?
- java - GSON 生成 gson 无法读取的 JSON
- react-native - 无法初始化类 org.codehaus.groovy.runtime.InvokerHelper - React Native
- vba - 神秘地改变变量类型