所以我有这个文本文件由数字和单词组成,例如像这样 - 09807754 18 n 03 aristocrat 0 blue_blood 0 patrician我想拆分它,以便每个单词或数字都会作为一个新行出现.
一个空白分隔符是理想的,因为我希望带有破折号的单词保持连接.
这是我到目前为止:
f = open('words.txt', 'r')
for word in f:
print(word)
Run Code Online (Sandbox Code Playgroud)
我不确定如何离开这里,我希望这是输出:
09807754
18
n
3
aristocrat
...
Run Code Online (Sandbox Code Playgroud) 我有一行从多个列表中提取变量,我希望它避免出现StopIteration错误,以便它可以移动到下一行.目前我正在使用break函数,这避免了StopIteration,但只给了我列表中的第一项,如果我要将它打印出来,它会留下一个空白行.
以下是我的两个具有相同问题的迭代.
def compose_line5(self, synset_offset, pointer_list):
self.line5 = ''''''
for item in pointer_list:
self.line5 += '''http://www.example.org/lexicon#'''+synset_offset+''' http://www.monnetproject.eu/lemon#has_ptr '''+pointer_list.next()+'''\n'''
break
return self.line5
def compose_line6(self, pointer_list, synset_list):
self.line6 = ''''''
for item in synset_list:
self.line6 += '''http://www.example.org/lexicon#'''+pointer_list.next()+''' http://www.monnetproject.eu/lemon#pos '''+synset_list.next()+'''\n'''
break
return self.line6
Run Code Online (Sandbox Code Playgroud)
这是我没有休息时得到的错误:
Traceback (most recent call last):
File "wordnet.py", line 225, in <module>
wordnet.line_for_loop(my_file)
File "wordnet.py", line 62, in line_for_loop
self.compose_line5(self.synset_offset, self.pointer_list)
File "wordnet.py", line 186, in compose_line5
self.line5 += '''http://www.example.org/lexicon#'''+self.synset_offset+''' http://www.monnetproject.eu/lemon#has_ptr '''+self.pointer_list.next()+'''\n'''
StopIteration
Run Code Online (Sandbox Code Playgroud)
有没有快速解决这个问题,或者我必须捕获我使用iter()的每个方法的异常?
我试图找到一种方法,如果它包含如下所示的任何字符串,将检查程序正在读取的文件的名称.我不确定这是否是正确的方法.该字符串将成为一个全局变量,因为我必须稍后在程序中使用它
class Wordnet():
def __init__(self):
self.graph = Graph()
self.filename = ''
self.word_type = ''
def process_file(self):
self.filename = "noun.txt"
self.file = open(self.filename, "r")
return self.file, self.filename
def check_word_type(self, filename):
if 'noun' in filename:
self.word_type = 'noun'
elif 'verb' in filename:
self.word_type = 'verb'
elif 'vrb' in filename:
self.word_type = 'verb'
elif adj in filename:
self.word_type = 'adj'
elif adv in filename:
self.word_type = 'adv'
else:
self.word_type = ''
return self.word_type
if __name__ == '__main__':
wordnet = Wordnet()
my_file = wordnet.process_file() …Run Code Online (Sandbox Code Playgroud) RDFlib可以将xml代码行放入python图形吗?我知道RDFlib通常使用三元组,但是如果我不必将它们从XML转换为Turtle,它将节省大量工作。
我有一行文字,像这样:
http://www.example.org/lexicon#13797906 http://www.monnetproject.eu/lemon#gloss an overwhelming number or amount; "a flood of requests"; "a torrent of abuse"
Run Code Online (Sandbox Code Playgroud)
我想将它分成三个部分,但只需使用前两个空格作为它们应该被拆分的点.这是我正在寻找的结果:
http://www.example.org/lexicon#13797906
http://www.monnetproject.eu/lemon#gloss
an overwhelming number or amount; "a flood of requests"; "a torrent of abuse"
Run Code Online (Sandbox Code Playgroud)
我曾教过maxsplit可以工作,但我不知道如何在这种情况下使用它
我有两个具有相同问题的测试方法,这里是主类中的原始方法:
def get_num_words(self, word_part):
""" 1 as default, may want 0 as an invalid case """
if word_part[3] == '0a':
self.num_words = 10
else:
self.num_words = int(word_part[3])
return self.num_words
def get_num_pointers(self, before_at):
self.num_pointers = int(before_at.split()[-1])
return self.num_pointers
Run Code Online (Sandbox Code Playgroud)
以下是两个测试类:
def test_get_num_words(self):
word_part = ['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', …Run Code Online (Sandbox Code Playgroud) 所以我有这个由数字和单词组成的文本(wordnet)文件,例如像这样 -
"09807754 18 n 03 aristocrat 0 blue_blood 0 patrician"
Run Code Online (Sandbox Code Playgroud)
我想在第一个数字中读取后续单词的字典名称(或列表).它的布局永远不会改变,它始终是一个8位数的键,后跟一个两位数字,一个字母和一个两位数字.最后两位数字(03)表示有多少单词(在这种情况下为三个单词)与前8位数字键相关联.
我的想法是,我会搜索字符串中的第14位并使用该数字运行循环来挑选与该键相关的所有单词
所以我认为它会像这样
with open('nouns.txt','r') as f:
for line in f:
words = range(14,15)
numOfWords = int(words)
while i =< numOfWords
#here is where the problem arises,
#i want to search for words after the spaces 3 (numOfWords) times
#and put them into a dictionary(or list) associated with the key
range(0,7) = {word(i+1), word(i+2)}
Run Code Online (Sandbox Code Playgroud)
从技术上讲,我正在寻找其中任何一个更有意义:
09807754 = { 'word1':aristocrat, 'word2':blue_blood , 'word3':patrician }
or
09807754 = ['aristocrat', 'blue_blood', 'patrician'] …Run Code Online (Sandbox Code Playgroud) 我有一个带有大量随机单词和数字的长文本行,我希望将一个变量分配给该行中唯一的3位数字.
数字会改变每一行,但总是只有3位数.如何在linepython中搜索唯一的3位数字?可能有3个字母的单词,所以它必须只是数字.
09824747 18 n 02 archer 0 bowman 0 003 @ 09640897 n 0000
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我想要变量数字= 003
python ×8
dictionary ×1
iteration ×1
list ×1
rdf ×1
split ×1
string ×1
unit-testing ×1
wordnet ×1
xml ×1