我有这段代码可以从文本文件中提取每一行,如下所示:
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相当陌生,所以其中一些对我来说很复杂