0

我有这段代码可以从文本文件中提取每一行,如下所示:

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"

它给了我如何声明变量的错误。下面的代码被缩短了,因为它太长了,不能在这里发布。

#!/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)

当我尝试运行此文件时,出现此错误

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

第 48 行和第 225 行显示在上面的块注释中。我不知道这是否是我声明自变量的某种方式,或者我将它们传递给每个方法的方式,甚至不知道它是如何被调用的,__main__但我只是不知道我在这里做错了什么。我对python相当陌生,所以其中一些对我来说很复杂

4

2 回答 2

2

“NameError: global name 'word_part' is not defined”告诉你python找不到名为“word_part”的全局变量,所以你需要在第48行用self.word_part替换word_part。

于 2013-06-24T15:11:44.043 回答
1

错误不在于self. .... 它与全局变量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

是从哪里来world_part的?

  • 如果它真的是全局变量,它应该在类之外定义:

    wold_part = this_or_that # initialize
    
    class Wordnet():
        def __init__(self):
            ...
    
        def line_for_loop(self, file):
            ...
            self.process_lex_filenum(word_part) # use
    
  • 如果它是一个模块变量,也就是在另一个模块的全局范围内定义的变量,你应该导入它:

    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
    
  • 如果它是一个实例变量,你应该从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
    
于 2013-06-24T15:10:49.323 回答