简体   繁体   English

无法在函数之间传递变量中的数据

[英]Unable to pass data in variables between functions

I am fairly new to Python. 我对Python很新。 I am familiar with the concept to pass data across functions. 我熟悉跨功能传递数据的概念。

In theory, 理论上,

def c():
   r = raw_input("Ask Something? ")
   ..
   return r

def p(x):
    ...
    do something

r = c()
p(r)

The code below works just fine via Terminal ( python filename.py file.txt ), but I want to add workflow where a variable stores the path to the file and passes it to the function ( processFile ). 下面的代码通过Terminal(python filename.py file.txt)工作正常,但我想添加工作流,其中变量存储文件的路径并将其传递给函数(processFile)。 I just cant get the data / value passed to the function. 我只是无法将数据/值传递给函数。

This is the code I am trying to edit : 这是我试图编辑的代码:

def registerException(exc):
    exceptions[exc] += 1

def processFile(x):
  with open(x, "r") as fh:
    currentMatch = None
    lastLine = None
    addNextLine = False
    for line in fh.readlines():
      if addNextLine and currentMatch != None:
         addNextLine = False
         currentMatch += line
         continue
      match = REGEX.search(line) != None
      if match and currentMatch != None:
         currentMatch += line
      elif match:
         currentMatch = lastLine + line
      else:
         if currentMatch != None:
            registerException(currentMatch)
         currentMatch = None
      lastLine = line
      addNextLine = CONT.search(line) != None
    # If last line in file was a stack trace
    if currentMatch != None:
      registerException(currentMatch)

for f in sys.argv[1:]:
  processFile(f)

for item in sorted(exceptions.items(), key=lambda e: e[1], reverse=True):
  print item[1], ":", item[0]

It doesnt matter if I declare the variable as Global or local. 如果我将变量声明为Global或local,则无关紧要。 Could someone please help me solve this issue? 有人可以帮我解决这个问题吗?

Edit 1 : 编辑1:

I have applied the changes Daniel Suggested and now I am getting : TypeError: 'NoneType' object is not iterable. 我已经应用了Daniel Suggested的更改,现在我得到了: TypeError:'NoneType'对象不可迭代。

Below is the code : 以下是代码:

def c():
    path = raw_input("Path to file? ")
    r = os.path.abspath(path)

def process_file(filename):
    current = None
    last_line = None
    continue_line = False
    with open(filename, "r") as fh:
        for line in fh:
            if continue_line and current is not None:
               continue_line = False
               current += line
               continue
            if REGEX.search(line):
               if current is None:
                  current = last_line
               current += line
            else:
               if current is not None:
                  yield current
               current = None
            last_line = line
            continue_line = CONT.search(line)
        # If last line in file was a stack trace
        if current is not None:
            yield current

def process_files(filenames):
    exceptions = defaultdict(int)
    for filename in filenames:
        for exc in process_file(filename):
            exceptions[exc] += 1

for item in sorted(exceptions.items(), key=lambda e: e[1], reverse=True):
    print item[1], ":", item[0]

r = c()
process_files(r)

I have made some changes and removed sys.argv[1] since it requires an argument at the command line when running the script. 我做了一些更改并删除了sys.argv [1],因为它在运行脚本时需要在命令行中使用参数。

I think the new error I am getting is due to the OS Path. 我认为我得到的新错误是由于OS Path。 How can I fix this ? 我怎样才能解决这个问题 ?

exceptions is also a parameter, that has to be transferred to all functions. exceptions也是一个参数,必须转移到所有函数。 Alternatively you can write processFile as generator, an inline registerException : 或者,您可以将processFile编写为生成器,内联registerException

def process_file(filename):
    current = None
    last_line = None
    continue_line = False
    with open(filename, "r") as fh:
        for line in fh:
            if continue_line and current is not None:
                continue_line = False
                current += line
                continue
            if REGEX.search(line):
                if current is None:
                    current = last_line
                current += line
            else:
                if current is not None:
                    yield current
                current = None
            last_line = line
            continue_line = CONT.search(line)
        # If last line in file was a stack trace
        if current is not None:
            yield current

def process_files(filenames)
    exceptions = defaultdict(int)
    for filename in filenames:
        for exc in process_file(filename):
            exceptions[exc] += 1

    for item in sorted(exceptions.items(), key=lambda e: e[1], reverse=True):
        print item[1], ":", item[0]

process_files(sys.argv[1:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM