-2
我可以编写非常基本的python级别。我发现这个蟒3脚本(argv的1和2是input.txt中和output.txt的):如何将此python 3代码转换为python 2.7?
import sys
inf = open (sys.argv[1], 'r')
ouf = open (sys.argv[2], 'w')
def summarize(data:list, crit:float, run:int)->list:
i = 0
s = []
while i < len(data):
j = i
while (j < len(data)) and (crit <= data[j]):
j += 1
if run <= j - i:
s.append('{}-{}'.format(i+1, j))
i = j+1
return s
def transfer(head:str, data:list, sink:open, crit:float, run:int)->None:
summary = summarize(data, crit, run)
if summary:
sink.write('{}: {}\n'.format(head, ', '.join(summary)))
def collect(source:open, sink:open, crit:float = 0.5, run:int = 1)->None:
data = head = []
for line in source:
L = line.strip()
if not L.startswith('#'):
data.append(float(L.split()[-1]))
else:
transfer(head, data, sink, crit, run)
data = []
head = L[1:].strip()
transfer(head, data, sink, crit, run)
collect(inf, ouf)
inf.close()
ouf.close()
,其转换(input.txt中)文件,例如:
# IUPred
# Copyright (c) Zsuzsanna Dosztanyi, 2005
#
# Z. Dosztanyi, V. Csizmok, P. Tompa and I. Simon
# J. Mol. Biol. (2005) 347, 827-839.
#
#
# Prediction output
# NP_414578.2
1 M 0.4476
2 S 0.5286
3 E 0.7328
4 S 0.8019
5 L 0.2094
6 H 0.2503
7 L 0.1791
8 T 0.9193
# IUPred
# Copyright (c) Zsuzsanna Dosztanyi, 2005
#
# Z. Dosztanyi, V. Csizmok, P. Tompa and I. Simon
# J. Mol. Biol. (2005) 347, 827-839.
#
#
# Prediction output
# NP_418290.4
1 M 0.5328
2 E 0.8759
3 S 0.5323
4 W 0.1942
5 L 0.2575
6 I 0.8823
7 P 0.8034
8 A 0.2258
9 A 0.2541
10 P 0.8783
11 V 0.1002
12 T 0.3583
到( output.txt)文件为:
NP_414578.2: 2-4, 8-8
NP_418290.4: 1-3, 6-7, 10-10
也就是说,它列出了其最后一列中值> = 0.5的范围(第一列)。
事实是,这将是理想的解析我的数据的脚本,这些脚本是使用预先发布的python2编写的python软件包生成的。要将它与这些预先存在的工具一起添加,我需要将上面的脚本转换为python2。
我尝试使用py3to2,但它没有给出任何不同的脚本。
请建议我如何可以将其转换到Python 2
谢谢
下面是一个疯狂的想法:尝试在Python 2上运行它,并检查它是否工作。它不?使用您最喜爱的搜索引擎来检查解决方案,尝试自己修复它。没有工作?在这里发布问题。提示:仔细看看Python 2和3如何处理文件。 –
只删除方法签名中的显式类型 – RafaelC
它显示以下错误,然后我运行py2.7:文件“script.py”,第5行 def summarize(data:list,crit:float,run:int) - > list: ^ SyntaxError:无效的语法 –