获取所有不以字符开头的字符串的最简单方法是什么?

发布于 2024-11-25 15:30:40 字数 302 浏览 2 评论 0原文

我正在尝试从文本文件中解析大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街女流氓 2024-12-02 15:30:40

使用生成器表达式,我认为最好的方法。

for line in (line for line in x if not line.startswith('?')):
    DO_STUFF

或者你的方式:

for line in x:
    if line.startswith("?"):
        continue
    DO_STUFF

或者:

for line in x:
    if not line.startswith("?"):
        DO_STUFF

这实际上取决于你的编程风格。我更喜欢第一个,但也许第二个看起来更简单。但我不太喜欢第三个,因为有很多缩进。

Use generator expressions, the best way I think.

for line in (line for line in x if not line.startswith('?')):
    DO_STUFF

Or your way:

for line in x:
    if line.startswith("?"):
        continue
    DO_STUFF

Or:

for line in x:
    if not line.startswith("?"):
        DO_STUFF

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.

喜爱皱眉﹌ 2024-12-02 15:30:40

这是一句很好的俏皮话,非常接近自然语言。

字符串定义:

StringList = [ '__one', '__two', 'three', 'four' ]

执行操作的代码:

BetterStringList = [ p for p in StringList if not(p.startswith('__'))]

Here is a nice one-liner, which is very close to natural language.

String definition:

StringList = [ '__one', '__two', 'three', 'four' ]

Code which performs the deed:

BetterStringList = [ p for p in StringList if not(p.startswith('__'))]
一个人的旅程 2024-12-02 15:30:40

像这样的东西可能就是你所追求的:

with open('myfile.txt') as fh:
  for line in fh:
    if line[0] != '?': # strings can be accessed like lists - they're immutable sequences.
      continue
    # All of the processing here when lines don't start with question marks.

Something like this is probably what you're after:

with open('myfile.txt') as fh:
  for line in fh:
    if line[0] != '?': # strings can be accessed like lists - they're immutable sequences.
      continue
    # All of the processing here when lines don't start with question marks.
淡忘如思 2024-12-02 15:30:40

类似于utdemir的答案:

from itertools import ifilterfalse  # just "filterfalse" if using Python 3

for line in ifilterfalse(lambda s: s.startswith('?'), lines):
    # DO STUFF

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:

from itertools import ifilterfalse  # just "filterfalse" if using Python 3

for line in ifilterfalse(lambda s: s.startswith('?'), lines):
    # DO STUFF

http://docs.python.org/library/itertools.html#itertools.ifilterfalse
http://docs.python.org/dev/py3k/library/itertools.html#itertools.filterfalse

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文