本文介绍了我怎么“偷看”进入下一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我正在从文件或标准输入读取行。

我想偷看进入下一行,如果它开始
带有特殊字符的
我想要打破for循环,

其他明智的我想做readline()。


有没有办法做到这一点?

例如:

而1:

line = stdin.peek_nextline()

如果不是行:break

如果行[0] ==" |":

break :

否则:

x = stdin.nextline()

#用x做点什么

谢谢

解决方案





你可以做的就是读取行而不管测试变量。


这里有一些示例代码(写下这个我的头脑所以语法

可能会关闭一些)


import re


file = open(" test.txt",''r'')


variablestr =''' '#this将是你的对象..在我的例子中使用一个字符串

的文件数据


file.seek(0,2)

eof = file.tell()#这是文件结尾的位置。

file.seek(0,0)


而file.tell()!= eof:

testline = file.readline()

如果re.match("#",testline)== True:

break

else:

variablestr + = testline


file.close()


现在,如果我担心在测试线的边缘,它会读取

,你可以做的就是在if中是这样的:

file.seek(( file.tell() - len(testline)),0)

这将使你回到该行的beginging,这就是之前的

readline在偷看之前打电话给你。


希望有所帮助..


Jeff




你可以做的就是阅读该行而不管测试变量。

这里有一些示例代码(写下我的头脑所以语法
可能会关闭一些)

导入重新

file = open( " test.txt",''r'')

variablestr =''''#这将是你的对象..在我的例子中使用字符串
作为文件数据

file.seek(0,2)
eof = file.tell()#what这是文件末尾的位置。
file.seek(0,0)

而file.tell()!= eof:
testline = file.readline()
如果re.match("#,testline)== True:
打破
其他:
variablestr + = testline

file.close()
现在,如果我担心自己正处于困境中它在你能做的内容中读取的测试行是if:例如:
file.seek((file.tell() - len(testline)),0)
这将使你回到那条线的边缘,这就是前一次电话中的readline在偷看之前离开你的地方。

希望能帮到一些......

杰夫
-




我注意到我的代码中有些东西。


re。匹配(#,testline)==真是不可能它会更像。

re.match("#",testline)!=无

Hi,
suppose I am reading lines from a file or stdin.
I want to just "peek" in to the next line, and if it starts
with a special character I want to break out of a for loop,
other wise I want to do readline().

Is there a way to do this?
for example:
while 1:
line=stdin.peek_nextline()
if not line: break
if line[0]=="|":
break:
else:
x=stdin.nextline()
# do something with x

thanks

解决方案




Well what you can do is read the line regardless into a testing variable.

here is some sample code (writting this off the topof my head so syntax
might be off some)

import re

file = open("test.txt", ''r'')

variablestr = '''' #this would be your object.. in my example using a string
for the file data

file.seek(0,2)
eof = file.tell() #what this is the position of the end of the file.
file.seek(0,0)

while file.tell() != eof:
testline = file.readline()
if re.match("#", testline) == True:
break
else:
variablestr += testline

file.close()

now if I was concerned with being at the beging of the testline that it read
in what you can do is in the if is something like:
file.seek((file.tell() - len(testline)), 0)
and that will move you back to the beginging of that line which is where the
readline from the previous call left you before the "peek".

hope that helps some..

Jeff




Well what you can do is read the line regardless into a testing variable.

here is some sample code (writting this off the topof my head so syntax
might be off some)

import re

file = open("test.txt", ''r'')

variablestr = '''' #this would be your object.. in my example using a string
for the file data

file.seek(0,2)
eof = file.tell() #what this is the position of the end of the file.
file.seek(0,0)

while file.tell() != eof:
testline = file.readline()
if re.match("#", testline) == True:
break
else:
variablestr += testline

file.close()

now if I was concerned with being at the beging of the testline that it
read in what you can do is in the if is something like:
file.seek((file.tell() - len(testline)), 0)
and that will move you back to the beginging of that line which is where
the readline from the previous call left you before the "peek".

hope that helps some..

Jeff
--
http://mail.python.org/mailman/listinfo/python-list



I noticed something in my code.

re.match("#", testline) == True isn''t possible it would be more like.
re.match("#", testline) != None


这篇关于我怎么“偷看”进入下一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:12
查看更多