Python字符串,使用查找特定单词然后复制单词并粘贴i

2024-05-29 07:14:51 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,伙计们,我有一个txt文件,如下所示:

parent text \n -reply to first text\n --reply to second text

现在我想要的是如下所示:

parent text\n - reply to parent text\n
-reply to parent text \n -- reply to second text\n

我知道我可以用python来做:

group = re.findall(r"--",data)

这将得到所有的-,后面没有文本,但由于我在每一个文本中有多个“-”,这让我对如何处理数据产生了困惑,有什么见解吗

编辑1: 这是我的数据

Why can\'t I find a girlfriend?\n-/u/remainenthroned \n-try tinder\n--r/incels\n--Maybe you should find a TreeHugger.\n---Got friendzoned there once, so nah\n----Trees are not so good with motion, you know.\n-----try grindr\n

在.split之后是这样的:

"Why can't I find a girlfriend?",
 '-/u/remainenthroned ',
 '-try tinder',
 '--r/incels',
 '--Maybe you should find a TreeHugger.',
 '---Got friendzoned there once, so nah',
 '----Trees are not so good with motion, you know.',
 '-----try grindr',

我想要的是:

"Why can't I find a girlfriend?" -/u/remainenthroned,
"Why can't I find a girlfriend?" -try tinder'
-/u/remainenthroned --r/incels',',

以此类推


Tags: totextyousofindreplycanparent

热门问题