gpt4 book ai didi

python - 抓取括号内的元素

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:32 24 4
gpt4 key购买 nike

如何获取括号内的元素并将它们放入文件中?

我(我)你(你)他(他)她(她)

提前致谢,阿迪亚

最佳答案

import re

txt = 'me (I) you (You) him (He) her (She)'
words = re.findall('\((.+?)\)', txt)

# words returns: ['I', 'You', 'He', 'She']
with open('filename.txt', 'w') as out:
out.write('\n'.join(words))

# file 'filename.txt' contains now:

I
You
He
She

关于python - 抓取括号内的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3882407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com