[英]Get specific words in a string
我是python的新手。 我有这个字符串
"DEALER: 'S up, Bubbless?
BUBBLES: Hey.
DEALER: Well, there you go.
JUNKIE: Well, what you got?
DEALER: I got some starters. "
我试图使所有以大写字母结尾的单词都以冒号结尾。 例如,我从上面的字符串中获得DEALER,BUBBLES和JUNKIE。 谢谢
这就是我尝试过的。 似乎可以工作。 但是没有我想要的那么准确。
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(':') ]
您需要摆脱重复。 一个不错的方法是搭配一套餐具。
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
这导致一组唯一的名称
set(['JUNKIE', 'DEALER', 'BUBBLES'])
import re
regex = re.compile( "(?P<name>[A-Z]*:)[\s\w]*" )
actors = regex.findall(text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.