簡體   English   中英

在聲明之前使用變量

[英]Use a variable before its declaration

我正在編寫Python腳本,但遇到了變量聲明問題,這是腳本的一部分:

if chromosome :
    if chromosome.group(1) != '1' :
        output.close()
        print str(base)+" bases found in chromosome "+chr_name
        chr_name = chromosome.group(1)
    output = open(current_dir+"/"+name+"3/"+name+".chr"+chromosome.group(1)+".wig", "w")
    output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")

如您所見,我在代碼中創建輸出之前先關閉了輸出,但實際上,這永遠不會發生,因為您無法在創建文件之前輸入條件。

我可以弄些臟東西,在我狀況不佳之前打開一個tmp文件,但這不是很優雅。 所以我想知道是否還有另一種解決方案可以使Python幸福?

編輯:

if chromosome :
    if chromosome.group(1) != '1' :
        print str(base)+" bases found in chromosome "+chr_name
        chr_name = chromosome.group(1)
    with open(current_dir+"/"+name+"3/"+name+".chr"+chromosome.group(1)+".wig") as output:
        output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")

編輯2:

這是算法的一個想法,也許可以幫助您更好地理解問題:

for line in input do :
     chromosome = re.search(something)
     if chromosome :
          if chromosome != '1' :
              ouput.close()
          output = open(file+chromosome)
          output.write(title)
     elif somethingElse :
          output.write(somethingElse)
     endif
endfor

我想創建其他文件(file1,file2,file3 ...)。 在創建file2之前,我必須關閉file1; 在創建file3之前,我必須關閉file2; 等等...但是對於第一個(file1),我不必關閉任何內容!

您可以使用with語句,像這樣

with open(current_dir+"/"+name+"3/"+name+".chr"+chromosome.group(1)+".wig", 'w') as output:
    output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")

您不必擔心顯式關閉文件。

您是否正在嘗試執行以下操作:

if chromosome :
    if chromosome.group(1) != '1' :
        print str(base)+" bases found in chromosome "+chr_name
        chr_name = chromosome.group(1)
    else:
        output = open(current_dir+"/"+name+"3/"+name+".chr"+chromosome.group(1)+".wig", 'w')
        output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")
        output.close()

        output = open("The next file")
        output.write("whatever")
        output.close()

或具有改進的文件處理和str.format而不是添加字符串:

if chromosome :
    if chromosome.group(1) != '1' :
        print str(base)+" bases found in chromosome "+chr_name
        chr_name = chromosome.group(1)
    else:
        with open({}/{}3/{}.chr{}.wig.format(current_dir,name,name,chromosome.group(1), 'w') as output:
            output.write("fixedStep chrom=chr"+chromosome.group(1)+" start=1 step=1\n")
        with open("The next file") as output:
            output.write("whatever")

這看起來像是itertools.groupby的候選者:

pattern = re.compile(...)
for chromasome, lines in itertools.groupby(input, key=pattern.search):
    with open(filebase + chromosome, 'w') as f:
        for line in lines:
            ...

我不確定您要的是什么,但我認為您可以使用另一個嵌套的if語句輕松解決此問題。

編輯:今天早上還為時過早,忘記了這是Python需要在對象中添加一些東西

output = None
    for line in input do :
         chromosome = re.search(something)
         if chromosome :
              if chromosome != '1' :
                  if output != None : 
                       output.close()
              output = open(file+chromosome)
              output.write(title)
         elif somethingElse :
              output.write(somethingElse)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM