函数在第二次运行后返回无

时间:2019-01-12 09:22:20

标签: python python-3.x function

为什么第二次调用grepcut()会返回None?

没有道理。这两行是相同的,唯一的区别是索引号(字段)。但是,如果我在两行之间交换位置,则第二行(现在是第一行)可以正确运行,但是第二行(以前是第一行,运行得很好)不会正确运行。

from termcolor import colored
from subprocess import call

def grepcut(inputRaw, grep, delimit, field):
    for line in inputRaw:
        if grep in line:
            output = line.split(delimit)[field]
            return output

def function():
    print(colored("[+] ", "green") + "Here we go!")
    with open('output1') as inputRaw:
        var1 = grepcut(inputRaw, grep = 'grep this', field = 6, delimit = " ")
        var2 = grepcut(inputRaw, grep = 'grep this', field = 3, delimit = " ")
        call(["clear"], shell=True)
        print("\n")
        print(var1, var2)
        #if var1 or var2 is None:
        #   print(colored("[-] ", "red") + "Not found!!!")
        #else:
        #   print(var1, var2)

function()

顺便说一下,这是output1的内容:

  

A B C D E F G
  H I J grep this K L M N
  O P Q R S T U

此代码的输出是:
L没有

0 个答案:

没有答案