Python - 全局变量未定义(但实际上已定义)

-3 投票
3 回答
554 浏览
提问于 2025-04-28 02:33

我正在处理一个把CSV文件转换成JSON格式的程序,但我总是遇到一个错误信息,搞不清楚问题出在哪里。缩进看起来是对的,所以我有点迷茫,不知道该怎么解决。下面是代码:

错误追踪信息(最近的调用在最前面):

  File "/home/uwp/widgets/contentFreshness/freshmap.py", line 308, in <module>
    main()
  File "/home/uwp/widgets/contentFreshness/freshmap.py", line 303, in main
    mySite.writeJSONFile(options)
  File "/home/uwp/widgets/contentFreshness/freshmap.py", line 247, in writeJSONFile
    outputFile.write('"' + str(dateOfCrawl) + '"' )
NameError: global name 'dateOfCrawl' is not defined

代码

class Site:

    dateOfCrawl = 0;

    def __init__(self,csvFilePath):
        self.pageList = [] # ordered list of page IDs
        self.pageData={} # dictionary of individual page dictionaries, indexed on page ID
        self.titleDict = { } # dictionary of unique titles
        self.buildPageData(csvFilePath)
        self.homePageId=self.pageList[0] # only use of site.pageList
        self.depth=0

    def buildPageData(self,csvFilePath):
        global dateOfCrawl
        # read data from CSV file, build a dictionary of page data, including list of children, in order
        lines = csv.reader(open(csvFilePath, "rb"))
        for line in lines:
            pageURL=line[0]
            pageURL=re.sub('\/\Z', '',pageURL) # remove any trailing slash
            self.pageData[pageURL]={}
            self.pageData[pageURL]["URL"]=pageURL
            self.pageData[pageURL]["Title"]=self.cleanTitle(line[1],pageURL)

            # when taking the home page and chop its url the parent will be http:/
            # which should be avoided by setting it to ''
            parent = chopPath(pageURL)
            if(parent == 'http:/'):
                parent=''
                dateOfCrawl = line[2]
            self.pageData[pageURL]["Parent"]= parent
            self.pageData[pageURL]["Modified"]=line[2]
            self.pageData[pageURL]["Children"]=[] 

        list = self.pageData.keys()

        # sort IDs before attempting to match children
        self.pageList = self.pageData.keys()
        self.pageList.sort()

        lineCount = 0       
        for pageURL in self.pageList:
            # record page as child of its parent (parents must already be in the list!)
            parentURL=self.pageData[pageURL]["Parent"]

            if (lineCount > 0):
                while( self.pageData.has_key(parentURL)== False):
                    if(parentURL == ''):
                        sys.exit(pageURL + " has no parent at " + parentURL)
                    parentURL = chopPath(parentURL)
                self.pageData[parentURL]["Children"].append(pageURL)

            lineCount+=1
        self.pageCount=lineCount

    def writeJSONFile(self,options):
        global dateOfCrawl
        outputFile = options ["outputFile"]
        #see http://code.google.com/intl/en/apis/visualization/documentation/reference.html#DataTable
        outputFile.write('[')
        outputFile.write('"' + str(dateOfCrawl) + '"' )
        self.homePage.toJSON(options)
        outputFile.write(']')
        outputFile.close()
暂无标签

3 个回答

-1

你需要定义一个叫 global dateOfCrawl 的变量。把 dateOfCrawl = 0; 这行代码放到 Site 类的 __init__ 方法里面,像这样:

class Site:
    def __init__(self,csvFilePath):
        global dateOfCrawl
        dateOfCrawl = 0
        ....
0

你可以这样访问这个变量:

Site.dateOfCrawl

Python中的静态类变量

0

你把 dateOfCrawl = 0; 设定为一个 类属性。但不知为什么,你还混用了一个 global,这两者是完全不同的东西。其实不太清楚为什么 dateOfCrawl 应该是一个类属性(而不是实例属性),或者为什么你还要用到 global。你可能应该这样做:

  1. 把它改成实例属性(把 dateOfCrawl = 0 放到 __init__ 方法里面);然后
  2. 在其他方法中访问 这个 属性(用 self.dateOfCrawl)。

撰写回答