- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一段代码,其中在遍历文件中的行的 for
循环中,我有一些 file.readline()
语句。尽管脚本似乎运行良好,但这对我来说似乎很有问题。为了演示,假设您的文件中有以下内容:
...bla bla
Header signifying important stuff are coming
important stuff line 1
important stuff line 2
important stuff line 3
...bla bla
此标题和重要内容行 block 出现多次,因此我的脚本看起来大致如下:
with open(file, 'r') as fi:
for line in fi:
if header in line:
a = fi.readline()
b = fi.readline()
c = fi.readline()
continue
for line in fi
和 fi.readline()
都消耗相同的对象(文件),因此在 continue
之后, line
的下一个值将是向下 4 行,因为 readline()
调用消耗了 3 行。
正如我所说,上述方法有效,但我并不是“如果有效,就不愚蠢”运动的大力支持者。
另一方面,另一种方法是:
found_header = True
with open(file, 'r') as fi:
for line in fi:
if header in line:
new_stuff_inc = []
found_header = True
elif found_header:
new_stuff_inc.append(line)
if len(new_stuff_inc) == 3:
found_header = False
这似乎更安全,因为文件一次喂一张嘴(只有for line..
消耗它),但它看起来不必要地复杂..
那么,应该如何去做这样的事情呢?
最佳答案
混合 for
和 readline()
不是很令人满意,你是对的,顺便说一句,它不起作用,所以第一个片段的问题解决了:) (使用 Python 2.7 进行测试)
Traceback (most recent call last):
File "<module1>", line 3, in <module>
ValueError: Mixing iteration and read methods would lose data
Python 只是不会接受逐行迭代文件和 readline(有点像迭代列表并从中删除元素,只不过这里它是 protected ) )
创建一个能够单独使用 for
的状态机既麻烦又容易出错。
你可以尝试另一种方法:
with open(file, 'r') as fi:
while True:
line = fi.readline()
if not line: # end of file
break
if header in line:
a = fi.readline()
b = fi.readline()
c = fi.readline()
continue
关于python - 循环遍历文件并同时使用 `file.readline()` 是 "ok"的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40915421/
这个问题在这里已经有了答案: What could be the reason that `require` doesn't work in some places? (3 个回答) 6 个月前关闭。
我正在使用读取行从维基百科获取一些文本。但读取行仅返回列表,而不是我想要的文本。有什么方法可以使用替代方案或解决我的问题吗? public class mediawiki { public s
我正在编写一小段代码,其中涉及使用子进程运行一个脚本来监听一些实时数据 这是我的代码: def subscriber(): try: sub = subprocess.Pope
我已包括: #include "stdio.h" #include #include 我的编译器包含标志 -lreadline 但我仍然收到错误消息: fatal error: 'readl
使用 Term::Readline::readline 停止无限循环的正确方法是什么? ? 这样我一个都看不懂 0 #!/usr/bin/env perl use warnings; use stri
标题比我的实际目标更具体: 我有一个使用 GNU Readline 的命令行程序,主要用于命令历史记录(即使用向上箭头检索以前的命令)和其他一些细节。现在,程序的输出似乎散布在用户的输入中,有时是可以
在 ipython 中,如果我按“esc”,然后按“enter”(可能还有其他字符?),读行会中断。我无法再使用“向上”键搜索命令历史记录,并且某些命令(例如 control-K)失败。 有没有办法在
我在使用 readlines() 和 readline() 返回值时遇到问题,但在使用 read() 时却没有。任何人都知道这是怎么发生的?欣赏一下 with open('seatninger.txt
标题比我的实际目标更具体: 我有一个使用 GNU Readline 的命令行程序,主要用于命令历史记录(即使用向上箭头检索以前的命令)和其他一些细节。现在,程序的输出似乎散布在用户的输入中,有时是可以
我正在编写一个聊天客户端,它必须在接收用户输入的同时输出接收到的消息。 到目前为止,我已经 fork 成两个独立的进程,其中一个继续监听套接字连接并用 printf 写出接收到的字符串。另一个使用 r
我在 NetworkStream 上使用 StreamReader,我只想读取一行或多行,而另一个数据是 byte array(如文件数据)我不想在 StreamReader 中读取该文件数据,例如我
我遇到了这两个 API,用于在 C# 的简单控制台应用程序中读取用户的输入: System.Console.ReadLine() System.Console.In.ReadLine() 这是一个我试
yum 我的系统显示已安装 readline rlwrap-0.41]$ sudo yum install readline Loaded plugins: fastestmirror, presto
我尝试做 this tutorial在 Rust 中,到目前为止,我在将 C 库连接到 Rust 时遇到了很多问题。 C 等效代码: #include #include #include #in
我正在寻找 web Python的标题中提到的命令及其区别;但是,我并不满足于对这些命令有完整的基本理解。 假设我的文件只有以下内容。 This is the first time I am posi
你如何在 F# 中使用 Console.Readline?与 Console.Writeline 不同,当我调用它时,它并没有受到尊重。 最佳答案 如果你使用 let s = Console.Read
在一次面试中,面试官问我为什么 readline() 比 Python 中的 readlines() 慢很多? 我回答的是readlines()需要多次读取,需要更多的开销。 不知道我的回答对不对。
要在 OSX Lion 上完全运行 ipython 需要什么?我试图让 ipython 与 readline 一起工作,但没有成功。 我的做法: (在虚拟环境中) pip install ipytho
在 Nodejs 文档中,我看到: import EventEmitter from 'events'; import { readFile } from 'fs'; import fs, { rea
我写了一个简单的应用程序: #include #include #include #include int main() { char *user_input; while(u
我是一名优秀的程序员,十分优秀!