2011-11-30 105 views
1

我測試我的python腳本:sh:-c:第1行:意外標記附近的語法錯誤`;'

#!/usr/bin/python 

import os,sys 
import glob 
import commands 
import gzip 
from itertools import islice 

f1=gzip.open("/home/xug/scratch/test_trim.fastq","w") 
LIST=[] 
N = 4 
with open("/home/xug/scratch/test.fastq", "r") as f: 
    while True: 
     line_group = list(islice(f, N)) 
     if not line_group: 
      break 
     l3=line_group[3].rstrip() 
     l3_trim=commands.getoutput("sed 's/\(.\)B*$/\1/g'" + l3) 
     #l3_to = subprocess.Popen(["sed 's/\(.\)B*$/\1/g'",l3], 
            #stdout=subprocess.PIPE,bufsize=1) 
     #l3_trim=l3_to.stdout 
     if (float(len(l3_trim))/float(len(l3)) > 0.70): 
       LIST.append(line_group[0]) 
       LIST.append(line_group[1][:int(len(l3_trim))]) 
       LIST.append(line_group[2]) 
       LIST.append(l3_trim) 

    output=f1.writelines(LIST) 

但是我得到了像錯誤:

sh: -c: line 0: unexpected EOF while looking for matching ``' 
sh: -c: line 1: syntax error: unexpected end of file 
sh: -c: line 0: unexpected EOF while looking for matching ``' 
sh: -c: line 1: syntax error: unexpected end of file 
sh: -c: line 0: unexpected EOF while looking for matching ``' 
sh: -c: line 1: syntax error: unexpected end of file 
sh: -c: line 0: unexpected EOF while looking for matching ``' 
sh: -c: line 1: syntax error: unexpected end of file 
sh: -c: line 0: unexpected EOF while looking for matching ``' 
sh: -c: line 1: syntax error: unexpected end of file 
sh: -c: line 0: unexpected EOF while looking for matching ``' 
sh: -c: line 1: syntax error: unexpected end of file 

最終杜絕while循環....

+0

從最基本的問題開始:'l3'包含什麼? –

+0

l3,是一個變量:l3 = list [3] – user815408

+1

我在你的代碼中看到(實際上是'l3 = line_group [3]')。但是,這仍然沒有告訴我們該變量的*內容*是什麼。如果你不知道,試試'print'語句。 –

回答

2

(從上面的評論繼續)

若要從字符串中刪除尾隨B使用Pyt hon的內置re模塊,請嘗試:

import re 

l3_trim = re.sub(r"B*$", "", l3) 
+0

哦,非常感謝....但我仍然好奇如何使用commands.getoutput + sed – user815408

+0

您必須引用shell元字符。請參閱[如何在Python中跳出os.system()調用?](http://stackoverflow.com/questions/35817/how-to-escape-os-system-calls-in-python)以獲取更多信息。 –

相關問題