What is the easiest way to get all strings that do not start with a character?

Viewed 111925

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? Thanks in advance for the help.

4 Answers

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('__'))]
Related