求助:python逐行读取,删除以某个字符开头的字符串
求助,怎么实现这个功能?感激不尽~输入文件示例:
OG0000000: aa|TbCa001702 aa|TbCa001961 aa|TbCa002485 bb|TbCa002794 bb|TbCa003025 cc|TbCa003100 dd|TbCa003129 ee|TbCa003206 bb|TbCa003139
OG0000001: aa|AT1G08070 aa|AT1G20230 bb|AT1G31920 bb|AT1G59720 bb|AT1G68930 cc|AT1G74630 cc|AT2G01510 dd|AT2G03880 dd|AT2G29760
OG0000002: aa|R002G044500 aa|R002G044700 bb|R002G044900 bb|R002G045000 bb|R002G079000 cc|R003G073100 dd|R004G270600 ee|R007G319800
输出结果:
OG0000000: aa|TbCa001702 aa|TbCa001961 aa|TbCa002485 bb|TbCa002794 bb|TbCa003025 bb|TbCa003139
OG0000001: aa|AT1G08070 aa|AT1G20230 bb|AT1G31920 bb|AT1G59720 bb|AT1G68930
OG0000002: aa|R002G044500 aa|R002G044700 bb|R002G044900 bb|R002G045000 bb|R002G079000
假如每行中冒号之后,以空格分割的字符串'|'管道符前面为cc, dd, ee,则删除这个字符串,其他的正常输出。
比如第一行中cc|TbCa003100 dd|TbCa003129 ee|TbCa003206 以cc, dd,ee开头,则删除,只输出其他的aa|TbCa001702 aa|TbCa001961 aa|TbCa002485 bb|TbCa002794 bb|TbCa003025 bb|TbCa003139 datas = ["OG0000000: aa|TbCa001702 aa|TbCa001961 aa|TbCa002485 bb|TbCa002794 bb|TbCa003025 cc|TbCa003100 dd|TbCa003129 ee|TbCa003206 bb|TbCa003139",
"OG0000001: aa|AT1G08070 aa|AT1G20230 bb|AT1G31920 bb|AT1G59720 bb|AT1G68930 cc|AT1G74630 cc|AT2G01510 dd|AT2G03880 dd|AT2G29760",
"OG0000002: aa|R002G044500 aa|R002G044700 bb|R002G044900 bb|R002G045000 bb|R002G079000 cc|R003G073100 dd|R004G270600 ee|R007G319800"]
list1 = []
for data in datas:
for each_group in data.split():
if not each_group.startswith(("ee", "dd", "cc")):
list1.append(each_group)
print(" ".join(list1))
list1 = []
"""
输出
OG0000000: aa|TbCa001702 aa|TbCa001961 aa|TbCa002485 bb|TbCa002794 bb|TbCa003025 bb|TbCa003139
OG0000001: aa|AT1G08070 aa|AT1G20230 bb|AT1G31920 bb|AT1G59720 bb|AT1G68930
OG0000002: aa|R002G044500 aa|R002G044700 bb|R002G044900 bb|R002G045000 bb|R002G079000
""" 大马强 发表于 2022-1-19 16:32
感谢您的帮助,祝新年快乐~ poppy章鱼 发表于 2022-1-19 16:56
感谢您的帮助,祝新年快乐~
新年快乐{:10_297:}
页:
[1]