嘿,伙计们,我有一个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',',
以此类推
如果数据在文件中
假设文件名是
text.txt
如果数据在变量中:
假设变量是
text
相关问题 更多 >
编程相关推荐