我是Python的新手,目前正在做一个小型测试用例分配,在其中我要查找字典关键字并将其与一个小型文本文件进行匹配,并查看这些密钥是否存在于文本文件中。 / p>
字典如下:
dict = {“ description,translation”:“ test_translation(serial,”,
“ unit”:“ test_unit(”,}
文本文件中的文本,此后称为“ requirement.txt”,如下所示:
说明应显示XXX的翻译。
该单元应隐藏。
从文件“ version.txt”中读取该值。
关键是,我要找到并匹配它们是否存在-匹配项应返回“测试通过”,没有匹配项将返回跳过。
字典中的键将被排序到一个列表中,然后进行迭代并与文本匹配。 (字典中的值将被排序到一个单独的列表中,并在一个单独的文件中进行迭代,在此我将不对其进行深入研究。)
这是我当前拥有(并停留)的代码:
list = sorted(key_words.keys(), key=lambda d: d[0])
with open('C:/Users-------/requirement.txt', 'r') as outfile:
lines = outfile.readlines()
for line in lines:
line = line.strip()
if line == '':
continue
line_strings = line.split(' ')
for word in list:
if word in line:
print("Test Pass")
print(word)
break
else:
print("Test Fail")
print(line + "\n")
当前获得的结果:
Test Fail
Test Pass
display
The description shall display the translation of XXX.
Test Fail
Test Fail
Test Fail
Test Pass
unit
The unit shall be hidden.
Test Fail
Test Fail
Test Fail
Test Fail
The value is read from the file "version.txt".
使用当前拥有的代码(并且被卡住),运行返回“测试通过”和“测试失败”多次的代码,这表明在每一行上多次重复键并返回结果对于每个多次迭代。
我被困在两个方面:
理想情况下,结果应以以下格式返回:
理想的结果:
('Text:', "The description shall display the translation of XXX.
('Key:', 'description, translation')
Test Pass
('Text:', 'The unit shall be hidden.')
('Key:', 'unit')
Test Pass
('Text:', 'The value is read from the file "version.txt".')
('Key:', (none))
Test Fail
谢谢您的启发!
答案 0 :(得分:1)
尝试一下:
list = sorted(key_words.keys(), key=lambda d: d[0])
with open('C:/Users-------/requirement.txt', 'r') as outfile:
lines = outfile.readlines()
for line in lines:
line = line.strip()
if line == '':
continue
# Create an empty list which will contain all the word that match
words_found = []
for word in list:
# if the word match then add it to the list words_found
if word in line:
words_found.append(word)
print("(\'Text:\',\"{}\"")' ".format(line))
print("(\'Keys:\',\"{}\"")' ".format(words_found))
# if the list of words found it's not empty then the test passed
if(words_found):
print("Test Passed")
else:
print("Test Failed")
想法是创建单词founds的列表,然后全部打印
我正在使用format操作,您可以找到有关如何使用它的指南here。行if(words_found):
检查列表是否为空。
在这种情况下,您将不需要它,但如果只想解决第二点,则可以使用for else
语句,如in the docs
4.4中断并继续执行语句,以及循环中的子句
循环语句可以包含else子句;当循环通过用尽列表而终止时(带有for)或条件变为假(具有while时),则执行此命令,但当循环由break语句终止时,则不执行该命令。
通过一个选项卡减少if语句的else缩进,它变成了for语句的else,因此仅在for从未中断问题解决的情况下才会执行。
list = sorted(key_words.keys(), key=lambda d: d[0])
with open('C:/Users-------/requirement.txt', 'r') as outfile:
lines = outfile.readlines()
for line in lines:
line = line.strip()
if line == '':
continue
line_strings = line.split(' ')
for word in list:
if word in line:
print(word)
print("Test Pass")
break
else:
print("Test Fail")
print(line + "\n")
要将键拆分为描述和翻译,我们只需使用内置函数split将逗号分隔成两个字即可
list = sorted(key_words.keys(), key=lambda d: d[0])
with open('C:/Users-------/requirement.txt', 'r') as outfile:
lines = outfile.readlines()
for line in lines:
line = line.strip()
if line == '':
continue
# Create an empty list which will contain all the word that match
words_found = []
for word in list:
description, translation = word.split(",")
# if the word match then add it to the list words_found
if description in line:
words_found.append(description)
print("(\'Text:\',\"{}\"")' ".format(line))
print("(\'Keys:\',\"{}\"")' ".format(words_found))
# if the list of words found it's not empty then the test passed
if(words_found):
print("Test Passed")
else:
print("Test Failed")