【发布时间】:2014-09-21 18:35:12
【问题描述】:
为我糟糕的编程表示歉意,因为我是一个糟糕的程序员。我参考:How to access next line in a file(.txt) in Python
在问题实例中,next() 模块能够读取下一行。
但是,一旦代码返回到 for 循环,就不会是“下一行”而是下一行了。
输入:
a /n
b /n
c /n
d /n
输出:
This line contains a, next line contains b.
This line contains c next line contains d.
有没有可能达到这样的结果
This line contains a, next line contains b
This line contains b next line contains c
编辑-抱歉忘记输入代码-示例代码:
a = open('file.txt','rb')
for i in a:
b = next(a)
print "this line contains %s, next line contains %s" %(a,b)
a.close()
【问题讨论】:
-
向我们展示您尝试达到预期结果的代码。
-
我假设发布问题的人对编程和堆栈溢出也是新手。因此,请不要只对问题投反对票,引导此人发布适当的问题并鼓励他的热情。
-
感谢您的热心回答