我得到了一个变量赋值错误,显然没有理解一些东西,但从我的代码中,我不明白为什么会发生这种情况。
代码:
def something(filename):
with open("tmp/"+filename.stem+".txt", "r") as infile: # opening the tmp file for scraping the data
readfile = infile.readlines() #reads the infile lne by line and returns a list containing the lines
for i, line in enumerate(readfile[1:], 1): # looping over all the lines in the file from position 1 (so skipping 0) to avoid circular feedback
if 'Overview' in line:
start = i
continue
for i, line in enumerate(readfile[1:], 1):
if 'Identified secondary metabolite regions using strictness' in line:
end = i
marker = list(map(lambda s: s.strip('\n'), readfile[start + 1:end])) # stripping the '\n' off every element in the list. map executes a function for each element in a sequence
for i, line in enumerate(readfile[1:], 1): # looping over all the lines in the file from position 1 (so skipping 0) to avoid circular feedback
for location in marker:
错误:
UnboundLocalError: local variable 'marker' referenced before assignment
由于第一个 forloop 在第二个 forloop 之前执行导致被设置,因此不应始终在函数范围内进行赋值?markermarker
其次,使用两个for循环似乎很混乱,我如何在一个for循环中执行此操作?
慕哥9229398
Smart猫小萌
缥缈止盈
忽然笑
随时随地看视频慕课网APP
相关分类