Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 脚本以删除行尾具有相同字符串的行,并将其余行以另一种格式保存在另一个文件中_Python - Fatal编程技术网

Python 脚本以删除行尾具有相同字符串的行,并将其余行以另一种格式保存在另一个文件中

Python 脚本以删除行尾具有相同字符串的行,并将其余行以另一种格式保存在另一个文件中,python,Python,我的文件如下所示: |#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.

我的文件如下所示:

|#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)|
|#Tail|head    |edge_weight|edge_type            |
|Q8TBF4|QQ15696|0.201461   |MI:0401 (biochemical)|
我想删除具有“MI:0004(亲和层析技术)”的行,并将另一行保存在另一个文件中。我还想将格式更改为类似以下内容:

|#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)|
|#Tail|head    |edge_weight|edge_type            |
|Q8TBF4|QQ15696|0.201461   |MI:0401 (biochemical)|
请注意,将原始边的权重更改为浮动,但在运行脚本时出错

我的剧本:

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
回溯(最近一次呼叫最后一次):
文件“E:\CIT656\pythonProjects\CIT656\u Spring21\Assignment\u 03.py”,第29行,在
边缘重量=浮动(行项目[2])
ValueError:无法将字符串转换为浮点值:“边\重量”
进程已完成,退出代码为1

我还没有测试过这一点,但尝试在
line\u items=line.split('\t')之后添加以下语句。

这将导致它跳过数据文件中的标题行

或者,您可以在打开文件后立即添加:

file_in_2.readline()

这将读入并忽略文件中的第一行。

尝试找出第29行上的语句可能试图将字符串
'edge\u weight'
转换为浮点的原因。它可能正在尝试解析标题文本吗?是的,使用“\t”可以考虑将问题标题更改为类似“解析文件中以制表符分隔的文本时出现ValueError”,因为这就是问题的具体原因。非常感谢您的帮助:)没问题。解决问题的信息在回溯消息中。始终仔细阅读回溯,并思考它发生的原因。通常这会导致理解问题以及如何解决问题。