Question

I am fairly new to python. I have a this string

"DEALER: 'S up, Bubbless?
BUBBLES: Hey.
DEALER: Well, there you go.
JUNKIE: Well, what you got?
DEALER: I got some starters. "

I am trying to get all the words in uppercase that end with a colon. For example, I get DEALER, BUBBLES and JUNKIE from the string above. Thanks

This is what i tried. Seems to work. But not as accurate as I would want.

s = "DEALER: 'S up, Bubbless? BUBBLES: Hey. DEALER: Well, there you go. JUNKIE: Well, what you got?DEALER: I got some starters.";
#print l
print [ t for t in s.split() if t.endswith(':') ]
Was it helpful?

Solution

You'll need to get rid of repeats. A nice way is with a set.

import re

mystring = """
DEALER: 'S up, Bubbless?
BUBBLES: Hey.
DEALER: Well, there you go.
JUNKIE: Well, what you got?
DEALER: I got some starters. """

p = re.compile('([A-Z]*):')
s = set(p.findall(mystring))

print s

This results in a unique set of names

set(['JUNKIE', 'DEALER', 'BUBBLES'])

OTHER TIPS

import re 

regex = re.compile( "(?P<name>[A-Z]*:)[\s\w]*" ) 

actors = regex.findall(text)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top