Question

I have this code that pulls in each line off a text file like this line:

13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005 @ 13796604 n 0000 + 00603894 a 0401 + 00753137 v 0302 + 01527311 v 0203 + 02361703 v 0101 | an overwhelming number or amount; "a flood of requests"; "a torrent of abuse"

and it is giving me errors with how the variables are declared. The code below is shortened as it is too long to post here.

#!/usr/bin/python
# -*- coding: utf-8 -*-
from rdflib import URIRef, Graph
from StringIO import StringIO

class Wordnet():

    def __init__(self):
        self.graph = Graph()
        self.before_at = ''
        self.after_at = ''
        self.word_part = ''
        self.gloss_part = ''
        self.lex_filenum = ''
        self.synset_offset = ''
.............more declarations(too long to add here).....

    def process_file(self):
        self.file = open("new_2.txt", "r")
        return self.file

    def line_for_loop(self, file):
        for line in file:
            self.split_pointer_part(line)
            self.split_word_part(line)
            self.split_gloss_part(line)
            self.process_lex_filenum(word_part)   ###error starts here on line 48
            self.process_synset_offset(word_part)
.............more methods(too long to add here).....

    def split_pointer_part(self, line):
        self.before_at, self.after_at = line.split('@', 1)
        return self.before_at, self.after_at    

    def split_word_part(self, line):
        self.word_part = line.split()
        return self.word_part

    def split_gloss_part(self, line):
        self.gloss_part = line.strip().split('|')
        return self.gloss_part

    def process_lex_filenum(self, word_part):
        self.lex_filenum = word_part(1)
        return self.lex_filenum

    def process_synset_offset(self, word_part):
        self.synset_offset = word_part[0]
        return self.synset_offset

............plus more................

def print_graph(self, graph):
        print graph.serialize(format='nt')

if __name__ == '__main__':
    wordnet = Wordnet()
    my_file = wordnet.process_file()
    wordnet.line_for_loop(my_file)   ###line 225
    wordnet.print_graph(graph)

When I try to run this file I get this error

File "wordnet.py", line 225, in <module>
    wordnet.line_for_loop(my_file)
  File "wordnet.py", line 48, in line_for_loop
    self.process_lex_filenum(word_part)
NameError: global name 'word_part' is not defined

Line 48 and 225 are shown in block comments above. I don't know if it is some way I have the self variables declared or the way I pass them into each method or even how it is called in __main__ but i just dont know what I have done wrong here. I am fairly new to python so some of this is complicated to me

Was it helpful?

Solution

"NameError: global name 'word_part' is not defined" is telling you that python cannot find a global variable named "word_part" so you would need to replace word_part with self.word_part on line 48.

OTHER TIPS

The error is not with a self. .... It is with the global variable world_part:

File "wordnet.py", line 225, in <module>
    wordnet.line_for_loop(my_file)
  File "wordnet.py", line 48, in line_for_loop
    self.process_lex_filenum(word_part)
NameError: global name 'word_part' is not defined

Where did world_part come from?

  • if it is really global variable it should be defined outside of the class:

    wold_part = this_or_that # initialize
    
    class Wordnet():
        def __init__(self):
            ...
    
        def line_for_loop(self, file):
            ...
            self.process_lex_filenum(word_part) # use
    
  • if it is a module variable, that is a variable defined if the global scope of an other module, you should import it:

    from my_world_module import world_part # import
    
    class Wordnet():
        def __init__(self):
            ...
    
        def line_for_loop(self, file):
            ...
            self.process_lex_filenum(word_part) # use
    
  • if it is an instance variable, you should reference it from self:

    class Wordnet():
        def __init__(self):
            self.world_part = this_or_that # initialize
    
        def line_for_loop(self, file):
            ...
            self.process_lex_filenum(word_part) # use
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top