我有一個字符串,它看起來像這樣:提取子從字符串在Python
catString = randomtextrandomtext (cats:1)=1 randomtext randomtextrandomtext (cats:3)=3 randomtext randomtext (cats:1540)=1,540 randomtextrandomtext randomtext (cats:85):85 randomtext
我想打印出一個字符串,看起來像這樣:
(cats:1)(cats:3)(cats:1540)(cats:85)
我試圖做:
catCount = ''
for a in catString:
for b in line.split():
if '(cats:' in b:
catCount += str(part)
print catCount
然而,該代碼打印出:
(cats:1)=1(cats:3)=3(cats:1540)=1,540(cats:85)=85
我該如何達到自己想要的效果?