我用这个格式文件的工作:有没有什么办法根据模式删除字符串中的重复字符串?
=Cluster=
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22491.xml;spectrum=1074 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=2950 true
=Cluster=
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=1876 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3479 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3785 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3785 true
=Cluster=
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22493.xml;spectrum=473 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22493.xml;spectrum=473 true
正如你可以看到,每一个SPEC线是不同的,但有两个地方重复串频谱的数量。我想要做的是将模式=Cluster=
之间的每一块信息,并检查是否有频谱值重复行。如果有多行重复,则除去一行。
输出文件应该是这样的:
=Cluster=
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22491.xml;spectrum=1074 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=2950 true
=Cluster=
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=1876 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3479 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3785 true
=Cluster=
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22493.xml;spectrum=473 true
我用groupby
从itertools模块里。我假设我的输入文件叫做f_input.txt,输出文件叫做new_file.txt,但是这个脚本也删除了SPEC的单词......而且我不知道我可以改变什么,以便不这样做。编号: 新的条件。有时部分行号可能会发生变化,例如:
=Cluster=
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=1876 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3479 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3785 true
SPEC PRD000682;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3785 true
正如您所看到的,最后一行已更改零件PRD号。一种解决方案是检查光谱数字,并根据重复频谱删除线条。
这将是一个解决方案:
=Cluster=
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=1876 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3479 true
SPEC PRD000681;PRIDE_Exp_Complete_Ac_22498.xml;spectrum=3785 true
你问为什么你的代码是不是会工作的任何代码工作还是? –
你可以尝试迭代整个文件并逐行检查,i = file.read()。split('\ n'),现在当我[1]在其他行像i [2]或i [3]时,然后删除我,然后对整个拆分的字符串逐个执行此操作。但是,它会是很多代码。我敢打赌会有一个很好的解决方案! –
你的代码工作正常,没有看到任何问题 –