簡體   English   中英

打開條件python文件,但從其中一個讀取數據

[英]open conditional python files but read data from one

我想根據條件打開並讀取文件,僅在條件滿足時才讀取。 我編寫了以下腳本:

def bb(fname, species):
     if species in ('yeast', 'sc'):
         pm =  open('file.txt', 'rU')
         for line in pm:
             line = line.split()
             with open(fname, 'rU') as user:
                 for e in user:
                     e = e.split()
                     if e[0] in line:
                         print(line)
    elif species in ('human', 'hs'):
         pm =  open('file2.txt', 'rU')
         for line in pm:
             line = line.split()
             with open(fname, 'rU') as user:
                 for e in user:
                     e = e.split()
                     if e[0] in line:
                         print(line)

有沒有適當的pythonic方式,我不必一遍又一遍地重復/寫相同的行(第3到10行)? 謝謝 !

您可以將文件名值放在變量中

def bb(fname, species):
    if species in ('yeast', 'sc'):
        fname2 = 'file.txt'
    elif species in ('human', 'hs'):
        fname2 = 'file2.txt'
    else:
        raise ValueError("species received illegal value")

    with  open(fname2, 'rU') as pm:
        for line in pm:
            line = line.split()
            with open(fname, 'rU') as user:
                for e in user:
                    e = e.split()
                    if e[0] in line:
                        print(line)

或定義另一個功能

def bb(fname, species):
    if species in ('yeast', 'sc'):
        read_file('file.txt', fname)
    elif species in ('human', 'hs'):
        read_file('file2.txt', fname)

def read_file(fname1, fname2):
    with  open(fname1, 'rU') as pm:
        for line in pm:
            line = line.split()
            with open(fname2, 'rU') as user:
                for e in user:
                    e = e.split()
                    if e[0] in line:
                        print(line)

由於無論條件如何,您似乎都在做完全相同的事情,因此您可以折疊所有內容嗎?

def bb(fname, species):
     if species in ['yeast', 'sc', 'human', 'hs']:
         pm =  open('file.txt', 'rU')
         for line in pm:
             line = line.split()
             with open(fname, 'rU') as user:
                 for e in user:
                     e = e.split()
                     if e[0] in line:
                         print(line)

要么就是您在復制代碼時犯了一個錯誤。 如果要根據情況進行不同的操作,則可以創建一個使用該參數的函數,或者先執行條件語句並使用它來設置某個字符串或值。

例如

if species in ('yeast', 'sc'):
    permissions = 'rU'

等等


編輯:啊,關於您編輯的問題,答案將如上,但隨后

if species in ('yeast', 'sc'):
    file_name = 'file.txt'
elif species in ('human', 'hs'):
    file_name = 'file2.txt'

只需打開文件, if else將以類似的方式以及相同的代碼塊進行其余操作。

def bb(fname, species):
    if species in ('yeast', 'sc'):
             pm =  open('file.txt', 'rU')
    elif species in ('human', 'hs'):
             pm =  open('file2.txt', 'rU')
         for line in pm:
             line = line.split()
             with open(fname, 'rU') as user:
                 for e in user:
                     e = e.split()
                     if e[0] in line:
                         print(line)

暫無
暫無

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

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