关于python:UnboundLocalError:赋值前引用的局部变量’strdate’

UnboundLocalError: local variable 'strdate' referenced before assignment

当我用3-4个文件来测试这个代码时,它工作得很好。但是当我运行3000多个文件时,错误消息突然出现,在extract数据中的第51行,文件"c:usersduldropboxarticleapfinal.py"。combid=matchcomp2+",+strdate+",+matchw+",+matchcountUnboundLocalError:在赋值之前引用了局部变量"strDate"

我搜索了一下,它看起来像是一个全球性的问题。我只是不明白那是什么意思。请帮忙。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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("
"
+combid)

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

似乎只有当len(matches)>0谓词为真时才分配strdate,如果是这样,请尝试在开始或在else子句内部添加默认strdate值进行调试。

似乎您正在尝试调用strdate,但由于if条件不为true,代码不知道strdate是什么(因为if语句为false,尚未分配它)。

这是我的猜测。


len(matches) < 0时失败

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

因此,如果条件语句失败,则无法设置strdate。然而,

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

取决于正在设置的,并假定它将始终被设置。

根据你想要达到的目标,你可以做几件事。这就是一个例子。

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


strdate仅为case len(matches)>0定义,但在分配给combid期间用于任何情况。