json - Python - global variable is not defined (but it is) -


Then I am working on this file which converts CSV to JSON, however I get this error message I can not but understand that indentation seems right, so why have I lost it to go with it. The code is below:

Traceback (the most recent call last):

  file "/ home / uwp / widgets / contentFreshness / freshmap Py ", line 308, & lt; Module & gt; In the file "/home/uwp/widgets/contentFreshness/freshmap.py", line 247, in the main () file "/home/uwp/widgets/contentFreshness/freshmap.py", line 303, main mySite.writeJSONFile (option) WriteJSONFile outputFile.write ('' + str (dateOfCrawl) + '' ') NameError: global name' dateOfCrawl 'is not defined  

code

class site: dateOfCrawl = 0; Def __init __ (self, csvFilePath): self.pageList = [] # Page id self.pageData list ordered = {} page person dictionaries, = {} unique title of self-indexed on the # page page self.titleDict # dictionary. BuildPageData (csvFilePath) self.homePageId = self.pageList [0] # only site.pageList self.depth = 0 Use of def buildPageData (self, csvFilePath): global dateOfCrawl #, read data from a CSV file, The page URL = line [0] pageURL = re.sub ('\ / \ Z', '', for the line in lines, the order line = csv.reader (open (csvFilePath, "rb")) , PageURL) # Remove any trailing slash yourself. Pageadata [page URL] = {} self PageData [page URL] ["url"] = page ur own Pagedata [page url] ["title"] = self Scantital (line [1], page url) # The parent should be http: / # while taking the home page and its URL (parent == 'http: /'): set to 'parent = choppath (pageURL)' Avoids doing so: parent = '' dateOfCrawl = line [2] self.pageData [pageURL] ["parent"] = Parents self page data [page URL] ["modified"] = line [2] Pageadata [page URL] ["child"] = [] list = self Pageadata.kiz () # Sort ID before attempting to match the child. Self.pageList = self .pageData.keys () self.pageList.sort () lineCount = 0 Page URL for self.pageList: # Records page as the parent of his parents (Parents are already on the list !) ParentURL = self.pageData [pageURL] ["guardian"] if (line count> 0): while (self.pageData.has_key (parentURL == incorrect): if (parent == ''): sys. Exit (pageURL + "no parent at parent URL) parentURL = choppath (parentURL) self page.Data [parent URL] [" child "] attached (PAGEURL) lineCount + = 1 self.pageCount = lineCount def writeJSONFile (auto, option ): Global dateOfCrawl outputfile = option ["outputfile"] #See http://code.google. Com / Intl / en / apis / view / documentation / reference.html # DataTable outputFile.write ('['] outputFile.write ('' + str (dateOfCrawl + '' ')) self.homePage.toJSON (option) OutputFile .write (')' outputfile.close ()

you dateoff crawl = 0; (Please read) for any reason as class attribute , you are also mixing in a global , which is completely different It's not really clear why dateOfCrawl is a class (instead of an example) attribute, or why you have global too. You should hardly:

  1. Make it a frequency attribute ( dateOfCrawl = 0 inside __init __ );
  2. Go to that in other ways ( self.dateOfCrawl )

Comments

Popular posts from this blog

c# - SignalR: "Protocol error: Unknown transport." when navigating to hub -

class - Kivy: how to instantiate a dynamic classes in python -

python - mayavi mapping a discrete colorbar on a surface -