我用這個格式文件的工作:有沒有什麼辦法根據模式刪除字符串中的重複字符串?
=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]時,然後刪除我,然後對整個拆分的字符串逐個執行此操作。但是,它會是很多代碼。我敢打賭會有一個很好的解決方案! –
你的代碼工作正常,沒有看到任何問題 –