2016-11-11 148 views
-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

謝謝

+1

下面是一個瘋狂的想法:嘗試在Python 2上運行它,並檢查它是否工作。它不?使用您最喜愛的搜索引擎來檢查解決方案,嘗試自己修復它。沒有工作?在這裏發佈問題。提示:仔細看看Python 2和3如何處理文件。 –

+0

只刪除方法簽名中的顯式類型 – RafaelC

+0

它顯示以下錯誤,然後我運行py2.7:文件「script.py」,第5行 def summarize(data:list,crit:float,run:int) - > list: ^ SyntaxError:無效的語法 –

回答

0

正如所有you..I的建議終於在結束了:

import sys 
inf = open (sys.argv[1], 'r') 
ouf = open (sys.argv[2], 'w') 


def summarize(data, crit, run): 
    'e' 
    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, data, sink, crit, run): 
    'et' 

    summary = summarize(data, crit, run) 
    if summary: 
     sink.write('{}: {}\n'.format(head, ', '.join(summary))) 

def collect(source, sink, crit = 0.5, run = 1): 
    'etc' 

    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() 

好像它作品。請讓我知道是否有什麼令人毛骨悚然的。

謝謝大家