获取所有不以字符开头的字符串的最简单方法是什么?
我正在尝试从文本文件中解析大约 2000 万行,并正在寻找一种方法来对不以问号开头的行进行一些进一步的操作。我想要一个不使用正则表达式匹配的解决方案。我想做的是这样的:
for line in x:
header = line.startswith('?')
if line.startswith() != header:
DO SOME STUFF HERE
我意识到 startswith
方法需要一个参数,但是有没有任何简单的解决方案可以从不以问号开头的行中获取所有行?
I am trying to parse about 20 million lines from a text file and am looking for a way to do some further manipulations on lines that do not start with question marks. I would like a solution that does not use regex matching. What I would like to do is something like this:
for line in x:
header = line.startswith('?')
if line.startswith() != header:
DO SOME STUFF HERE
I realize the startswith
method takes one argument, but is there any simple solution to get all lines from a line that DO NOT start with a question mark?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用生成器表达式,我认为最好的方法。
或者你的方式:
或者:
这实际上取决于你的编程风格。我更喜欢第一个,但也许第二个看起来更简单。但我不太喜欢第三个,因为有很多缩进。
Use generator expressions, the best way I think.
Or your way:
Or:
It is really up to your programming style. I prefer the first one, but maybe second one seems simplier. But I don't really like third one because of a lot of indentation.
这是一句很好的俏皮话,非常接近自然语言。
字符串定义:
执行操作的代码:
Here is a nice one-liner, which is very close to natural language.
String definition:
Code which performs the deed:
像这样的东西可能就是你所追求的:
Something like this is probably what you're after:
类似于utdemir的答案:
http://docs.python.org/library/itertools。 html#itertools.ifilterfalse
http://docs.python.org/dev/py3k/library /itertools.html#itertools.filterfalse
Similar to utdemir's answer:
http://docs.python.org/library/itertools.html#itertools.ifilterfalse
http://docs.python.org/dev/py3k/library/itertools.html#itertools.filterfalse