Python 无法在函数之间传递变量中的数据
我对Python相当陌生。我熟悉跨函数传递数据的概念 理论上,Python 无法在函数之间传递变量中的数据,python,python-2.7,function,variables,global-variables,Python,Python 2.7,Function,Variables,Global Variables,我对Python相当陌生。我熟悉跨函数传递数据的概念 理论上, def c(): r = raw_input("Ask Something? ") .. return r def p(x): ... do something r = c() p(r) 下面的代码通过终端(python filename.py file.txt)运行良好,但我想添加一个工作流,其中一个变量存储文件的路径并将其传递给函数(processFile)。我只是无法将数据/值传递给函数
def c():
r = raw_input("Ask Something? ")
..
return r
def p(x):
...
do something
r = c()
p(r)
下面的代码通过终端(python filename.py file.txt)运行良好,但我想添加一个工作流,其中一个变量存储文件的路径并将其传递给函数(processFile)。我只是无法将数据/值传递给函数
这是我试图编辑的代码:
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]
不管我是将变量声明为全局变量还是局部变量。有人能帮我解决这个问题吗
编辑1:
我已经应用了Daniel建议的更改,现在我得到了:TypeError:“NoneType”对象不可编辑。
代码如下:
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)
我做了一些更改并删除了sys.argv[1],因为它在运行脚本时需要在命令行中使用一个参数
我认为我得到的新错误是由于操作系统路径。我怎样才能解决这个问题 异常也是一个参数,必须传输到所有函数。或者,您可以将
processFile
编写为生成器,一个内联寄存器异常
:
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:])
异常
也是一个参数,必须传输到所有函数。或者,您可以将processFile
编写为生成器,一个内联寄存器异常
:
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:])
你在说什么变量?嗨,马克,如果我在processfile函数之前添加一个变量x=pathtofile,由于某种原因,该值没有被传递。我试着将其创建为全球和本地。我还尝试创建一个函数来捕获这个变量并将其传递给processfile函数,但结果仍然相同。代码将通过终端(python file.py log.txt)正常运行,但我们希望在代码中硬编码pathtofile。您在说什么变量?Hi Mark,如果我在processfile函数之前添加一个变量x=pathtofile,由于某种原因,值不会传递。我试着将其创建为全球和本地。我还尝试创建一个函数来捕获这个变量并将其传递给processfile函数,但结果仍然相同。代码将通过终端(python file.py log.txt)正常运行,但我们希望在代码中硬编码pathtofile。感谢您的输入。但最初的问题仍然存在。我知道它可以通过终端(python file.py log.txt)正常运行,但是我如何在代码中硬编码路径呢?我尝试先声明一个全局变量,然后声明一个局部变量,但process_file函数没有看到这个变量。您可以使用任何文件名列表调用
process_files
,即使是硬编码的文件名。谢谢Daniel!!多亏了你的建议,我终于成功了!!!我丢失了进程文件([r])!!!谢谢你的意见,丹尼尔。但最初的问题仍然存在。我知道它可以通过终端(python file.py log.txt)正常运行,但是我如何在代码中硬编码路径呢?我尝试先声明一个全局变量,然后声明一个局部变量,但process_file函数没有看到这个变量。您可以使用任何文件名列表调用process_files
,即使是硬编码的文件名。谢谢Daniel!!多亏了你的建议,我终于成功了!!!我丢失了进程文件([r])!!!