繁体   English   中英

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

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

当我使用3-4个文件尝试此代码作为测试时,它工作正常。 但是,当我运行3,000多个文件时,错误消息弹出,显示文件“ C:\\ Users \\ dul \\ Dropbox \\ Article \\ ap_final.py”的第51行,位于extract_data combid = matchcomp2 +“,” + strdate +“中, “ + matchw +”,“ + matchcount UnboundLocalError:赋值之前引用了局部变量'strdate'

我搜索了一下,看起来好像是全局问题。 我根本不明白那是什么意思。请帮助。

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的任何情况下都使用。

len(matches) < 0时失败

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

因此,如果您的条件语句失败, strdate不会设置strdate 然而,

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

取决于所设置的内容,并假设它将始终被设置。

根据您要实现的目标,您可以执行几项操作。 这样的例子之一。

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

看起来只有在len(matches)> 0谓词为true时才分配strdate,如果是这种情况,请尝试在else子句的开头或内部添加默认strdate值以进行调试。

看来您正在尝试调用strdate,但是由于if条件不成立,因此代码不知道strdate是什么(由于if语句为false,因此尚未分配strdate)。

那是我的猜测。

暂无
暂无

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

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