我的文件是这样的:
|#tail |head |edge_weight|edge_type |
|Q8TBF5|Q9UKB1 |3.11133e-01|MI:0004 (affinity chromatography technology)|
|Q8TBF4|QQ15696|2.01461e-01|MI:0401 (biochemical) |
|Q8TBF4|Q15696 |3.11133e-01|MI:0004 (affinity chromatography technology)|我想要删除具有'MI:0004 (亲和层析技术)‘的行,并将另一行保存在另一个文件中,我还想将格式更改为类似这样的内容:
|#Tail|head |edge_weight|edge_type |
|Q8TBF4|QQ15696|0.201461 |MI:0401 (biochemical)|请注意,将原始edge_weight更改为float,但在运行脚本时出现错误。
我的脚本:
from math import log
file_path_in_2 = 'D:/Courses/Bioinformatics Diploma/Programming to bioinformatics/Assignments/Assignment_03/PathLinker_PPI.txt'
file_in_2 = open(file_path_in_2, 'r')
file_path_out_2 = 'D:/Courses/Bioinformatics Diploma/Programming to bioinformatics/Assignments/Assignment_03/PathLinker_PPI_others.txt'
file_out_2 = open(file_path_out_2, 'w')
file_out_2.write('#tail|head\tedge_weight\tedge_type\n')
for line in file_in_2:
if 'MI:0004 (affinity chromatography technology)' not in line:
line_items = line.split('\t')
edge_weight = float(line_items[2])
edge_weight = 1 - (edge_weight)
new_string = '%s|%s\t%f\t%s' %(line_items[0], line_items[1], edge_weight, line_items[3])
file_out_2.write(new_string)
file_out_2.close()
file_in_2.close()我得到了这个错误:
Traceback (most recent call last):
File "E:\CIT656\pythonProjects\CIT656_Spring21\Assignment_03.py", line 29, in <module>
edge_weight = float(line_items[2])
ValueError: could not convert string to float: 'edge_weight'
Process finished with exit code 1发布于 2021-05-31 01:32:20
我还没有对此进行测试,但尝试在line_items = line.split('\t')之后添加以下语句
if line_items[0] == '#tail':
continue这将导致它跳过数据文件中的标题行。
或者您可以在打开文件后立即添加以下内容:
file_in_2.readline()这将读入并忽略文件中的第一行。
https://stackoverflow.com/questions/67763930
复制相似问题