简体   繁体   English

UnboundLocalError:分配前已引用局部变量“ strdate”

[英]UnboundLocalError: local variable 'strdate' referenced before assignment

When I tried this codes with 3-4 files as a test, it worked fine. 当我使用3-4个文件尝试此代码作为测试时,它工作正常。 But when I ran with 3,000+ files, the error message popped up saying File "C:\\Users\\dul\\Dropbox\\Article\\ap_final.py", line 51, in extract_data combid = matchcomp2 + "," + strdate + "," + matchw + "," + matchcount UnboundLocalError: local variable 'strdate' referenced before assignment 但是,当我运行3,000多个文件时,错误消息弹出,显示文件“ C:\\ Users \\ dul \\ Dropbox \\ Article \\ ap_final.py”的第51行,位于extract_data combid = matchcomp2 +“,” + strdate +“中, “ + matchw +”,“ + matchcount UnboundLocalError:赋值之前引用了局部变量'strdate'

I searched and it looks like something with global is an issue. 我搜索了一下,看起来好像是全局问题。 I simply do not understand what that means..Please help. 我根本不明白那是什么意思。请帮助。

import os,csv,datefinder,re
import numpy as np

os.chdir('C:\Users\dul\Dropbox\Article\parsedarticles')

def matchwho(text_to_match):
    if 'This story was generated by' in text_to_match:
        return('1')
    elif 'This story includes elements generated' in text_to_match:
        return('2')
    elif 'Elements of this story were generated' in text_to_match:
        return('2')
    elif 'Portions of this story were generated' in text_to_match:
        return('2')
    elif 'Parts of this story were generated' in text_to_match:
        return('2')
    elif 'A portion of this story was generated' in text_to_match:
        return('2')
    elif 'This sory was partially generated by' in text_to_match:
        return('2')
    elif 'This story contains elements generated by' in text_to_match:
        return('2')
    elif 'This story includes information generated by' in text_to_match:
        return('2')
    elif 'This story was originally generated by' in text_to_match:
        return('1')
    else:
        return('3')


def extract_data(filename):
    with open(filename, 'r') as file1:
        text1=file1.read()
#locate the date of the article
    matches = list(datefinder.find_dates(text1))
    if len(matches) > 0:
        date=matches[1]
        strdate = str(date)
    else:
        print 'No dates found'



#locate the name of the company2
    matchcomp2 = text1.split(' ', 1)[0]
#count the number of words in the article
    matchcount = re.search(r'(.*) words', text1).group(1).strip()
#determine the article
    matchw =str(matchwho(text1))
#list the returns in a line
    combid = matchcomp2 + "," + strdate + "," + matchw + "," + matchcount
#save in txt format
    with open('outfile.txt', "a+") as outfile:
        outfile.write("\n"+combid)

files = os.listdir("C:\Users\dul\Dropbox\Article\parsedarticles")
for file in files:
    if ".txt" in file:
        extract_data(file)

strdate仅在len(matches)> 0的情况下定义,但在分配给combid的任何情况下都使用。

This fails when len(matches) < 0 len(matches) < 0时失败

def extract_data(filename):
...
    if len(matches) > 0:
        date=matches[1]
        strdate = str(date)
    else:
        print 'No dates found'

So if your conditional statement fails, strdate never gets set. 因此,如果您的条件语句失败, strdate不会设置strdate However, 然而,

combid = matchcomp2 + "," + strdate + "," + matchw + "," + matchcount

is dependent on that being set and assumes it will always be set. 取决于所设置的内容,并假设它将始终被设置。

Based on what you want to achieve there is several things you can do. 根据您要实现的目标,您可以执行几项操作。 One such example is this. 这样的例子之一。

def extract_data(filename):
...
    if len(matches) > 0:
        date=matches[1]
        strdate = str(date)
    else:
        print 'No dates found in {}'.format(filename)
        strdate = ''

Looks like you are assigning strdate only when the len(matches) > 0 predicate is true, try to add a default strdate value either at the beginning or inside else clause to debug if that is the case. 看起来只有在len(matches)> 0谓词为true时才分配strdate,如果是这种情况,请尝试在else子句的开头或内部添加默认strdate值以进行调试。

It seems you are trying to call strdate but because the if condition is not true the code doesn't know what strdate is (since it hasnt been assigned yet due to the if statement being false). 看来您正在尝试调用strdate,但是由于if条件不成立,因此代码不知道strdate是什么(由于if语句为false,因此尚未分配strdate)。

That's my guess. 那是我的猜测。

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

相关问题 UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment unboundLocalError:赋值前引用了局部变量“loopback” - unboundLocalError: local variable 'loopback' referenced before assignment UnboundLocalError:分配前已引用局部变量“ endProgram” - UnboundLocalError: local variable 'endProgram' referenced before assignment UnboundLocalError:赋值前引用了局部变量“Score” - UnboundLocalError: local variable 'Score' referenced before assignment UnboundLocalError:分配前引用了局部变量“检查” - UnboundLocalError: local variable 'checking' referenced before assignment UnboundLocalError:在赋值之前引用了局部变量“区域” - UnboundLocalError: local variable 'region' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM