Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 308
drbunsen Avatar asked Jul 20 '11 14:07

drbunsen


People also ask

How do you check if a string starts with a specific character in Python?

Python String startswith() The startswith() method returns True if a string starts with the specified prefix(string). If not, it returns False .

How do I strip a character from a string?

Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.


2 Answers

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.

like image 106
utdemir Avatar answered Sep 20 '22 16:09

utdemir


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('__'))] 
like image 32
WalyKu Avatar answered Sep 20 '22 16:09

WalyKu