我有这段代码从文本文件中拉出每行:
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很新,所以其中一些对我来说很复杂
答案 0 :(得分:2)
“NameError:全局名称'word_part'未定义”告诉您python无法找到名为“word_part”的全局变量,因此您需要在第48行将self_word_part替换为word_part。
答案 1 :(得分: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