我有一个包含这种字符串的文件:
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
任何想法?
答案 0 :(得分:0)
您可以按字符串读取文件,并按空格分割每一行:
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的slicing syntax。根据上面的例子,这假定了两件事:
如果前者不是真的,你将拆分应该作为单个项目的令牌,如果后者不成立,你将遇到超出范围的异常。