[Tutor] Re: print out lines that start with a word
Wolfram Kraus
kraus at hagen-partner.de
Wed Feb 9 11:08:40 CET 2005
Liam Clarke wrote:
>>>>x= ["Dude", 'How is it going man', ' ']
>>>>print x
>
> ['Dude', 'How is it going man', ' ']
>
>
>>>>j=[]
>>>>for item in x:
>
> ... if re.search('\S+',item):
> ... j.append(item)
>
>
>>>>print j
>
> ['Dude', 'How is it going man']
What about:
x= ["Dude", 'How is it going man', ' ']
print [a for a in x if a.strip()]
> Now, I first did that in a Perl script, but it easily comes across to
> Python. \S+ will usually mean 'one or more non-whitespace characters'.
> Brilliant for when your first Perl script accidentally appended \n to
> everything, even \n and \t. *embarassed*
>
> Whereas, while I'm sure there is a string method that can do that, I'm
> not sure what it is.
Well the documentation on that is not sooo big ;-)
> Also - say you have a list of words -
> j = " bag apple tomato cheese *marmite* sausages *scones*"
>
> and you wanted to pick up each word that was asterisked.
> I did this as a string method then a regex.
>
>
>
>>>>try:
>
> ... indexes=[]
> ... lastIndex = 0
> ... while 1:
> ... x = j.index("*", lastIndex + 1)
> ... indexes.append(x)
> ... lastIndex = x
> ... except ValueError:
> ... pass
> ...
>
>>>>print indexes
>
> [4, 10]
>
>>>>myString = j[5:10]
>
That gives me [25, 33, 45, 52], propably a C&P bug?
> Now the regular expression -
>
>
>>>>x = re.finditer("\*(?P<myString>.*?)\*", j)
>>>>a = x.next()
>>>>print a.group('myString')
>
> apple
Same error as above? And you said you want _all_ asteriksed words!
How about this cute lil list comprehension:
print [w for w in j.split() if words[0] == '*' and words[-1] == '*']
> Now, while the regEx syntax is a big harsh to begin with, once you get
> the hang of it, it's OK, and the string method version I used felt too
> 'hacky' for me. Of course, both only work with pairs of asterisks, so
> I guess that makes them both hacky. : )
>
> That's my 2c, I use string methods for stuff like that .startswith, .endswith,
> if 'foo' in x stuff is good as well. But sometimes, a regex is the
> right tool for the job.
I didn't say that they are useless (and don't wanna troll or start a
flameware), but IMHO they are a PITA when it comes to debugging. Ever
touched a regexp after one year ;-)?
> Regards,
>
>
> Liam Clarke
Greetings,
Wolfram
More information about the Tutor
mailing list