我有一个带有这种字符串的文件:
gene_1 1 181 + 1 01 15.2689 b - - -
gene_2 220 300 - 0 01 0.649993 b - - -
gene_1 1 295 - 0 10 16.1465 b - - -
gene_1 39 278 + 0 11 13.4122 b 28 33 -0.698805
gene_1 1 133 - 0 10 13.9421 p 139 144 0.493777
我需要更改第五列(0和1)中的第四列(带有+和-的列)以获得下一个:
gene_1 1 181 1 + 01 15.2689 b - - -
gene_2 220 300 0 - 01 0.649993 b - - -
gene_1 1 295 0 - 10 16.1465 b - - -
gene_1 39 278 0 + 11 13.4122 b 28 33 -0.698805
gene_1 1 133 0 - 10 13.9421 p 139 144 0.493777
有任何想法吗?
您可以按字符串读取文件,然后按空格分隔每一行:
your_rearranged_lines = []
with open("yourFile") as file:
for line in file:
splitLine = line.split()
# stores the first three tokens as normal, switches the fourth and fifth,
# and stores the remaining tokens as normal
rearranged_line = splitLine[0:4] + splitLine[4] + splitLine[3] + splitLine[4:]
your_rearranged_lines.append(rearranged_line)
实质上,此代码使用python的切片语法。根据上面的示例,这假定了两件事:
如果前者不正确,则将令牌分割为一个项目;如果后者不正确,则将遇到超出范围的异常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句