[英]How do I check for next to next line while reading a file in python and strip the newline character at its end?
我有一个非常庞大的javascript文件,我正在尝试进行分析。 该文件有很多代码,其中删除了换行符,并且分析该文件变得越来越困难,因此我使用了replace函数来查找的所有实例;
并将其替换为;\
(\\ u000A是换行符的unicode)。 这解决了我的问题,程序变得更具可读性。 但是我现在有另一个问题: 每个for
循环都被更改了。
例如:
for(i=0; i<someValue; i++)
变成了
for(i=0;
i<someValue;
i++)
我想用Python编写程序来格式化此错误。 我的想法是:
for line in open('index.html', 'r+'):
if line.startswith('for(') and line.endswith(';'):
line.strip('\n')
但是,我不知道我用什么代码来剥离下一行换行符,因为for
循环一次只能读取一行。 有人可以建议我要做什么吗?
Python文件对象是可迭代的,您可以在循环时要求其下一行:
with open(inputfilename) as ifh:
for line in ifh:
if line.startswith('for(') and line.endswith(';\n'):
line = line.rstrip('\n') + next(ifh).rstrip('\n') + next(ifh)
这使用next()
函数从ifh
文件对象中检索接下来的两项并将其添加到当前行。 外循环将在此之后继续。
为了说明,请查看此迭代器循环的输出:
>>> lst = [1, 2, 3, 4]
>>> lst_iter = iter(lst)
>>> for i in lst_iter:
... print i
... if i == 2:
... print 'skipping ahead to', next(lst_iter)
...
1
2
skipping ahead to 3
4
在这里, next()
将lst_iter
迭代到下一个项目,然后外部的for
循环继续执行此后的下一个值。
您的下一个问题是就地重写文件。 您无法同时读取和写入同一文件,并希望只替换正确的部分。 缓冲和不同的行长会影响您的工作。
使用fileinput
模块来处理文件内容的替换:
import sys
import fileinput
for line in fileinput.input(inputfilename):
if line.startswith('for(') and line.endswith(';'):
line = line.rstrip('\n') + next(ifh).rstrip('\n') + next(ifh)
sys.stdout.write(line)
或使用我的就地文件重写上下文管理器 。
from inplace import inplace
with inplace(inputfilename) as (ifh, ofh):
for line in ifh:
if line.startswith('for(') and line.endswith(';'):
line = line.rstrip('\n') + next(ifh).rstrip('\n') + next(ifh)
ofh.write(line)
您可以使用一个计数器,如下所示:
cnt = 2
for line in open('index.html'):
if(line.startswith('for(') and line.endswith(';\n')):
cnt = 0
if cnt < 2:
line = line.strip('\n')
cnt += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.