简体   繁体   English

打开条件python文件,但从其中一个读取数据

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

I want to open and read a file depending on a condition, read only if condition met true. 我想根据条件打开并读取文件,仅在条件满足时才读取。 I wrote the following scriptlet: 我编写了以下脚本:

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)

Is there a proper pythonic way, where I don't have to repeat/write the same lines (line3 to 10) over and over again ? 有没有适当的pythonic方式,我不必一遍又一遍地重复/写相同的行(第3到10行)? Thanks ! 谢谢 !

You can put the filename value in a variable 您可以将文件名值放在变量中

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)

or define another function 或定义另一个功能

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)

Since you seem to be doing the exact same thing regardless of the condition, you could just collapse everything? 由于无论条件如何,您似乎都在做完全相同的事情,因此您可以折叠所有内容吗?

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)

Either that or you've made a mistake copying the code. 要么就是您在复制代码时犯了一个错误。 If you want to do something different depending on the case, then you could make a function that takes that argument, or do the conditional statement first and use it to set a certain string or value. 如果要根据情况进行不同的操作,则可以创建一个使用该参数的函数,或者先执行条件语句并使用它来设置某个字符串或值。

Eg 例如

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

etc. 等等


Edit: Ah, with your edited question the answer would be as above but then 编辑:啊,关于您编辑的问题,答案将如上,但随后

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

Just put opening of file in if else case rest will be done in a similar manner and same code block as well. 只需打开文件, 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