【问题标题】:Matching dict key to text file and returning Test Pass/fail将字典键匹配到文本文件并返回测试通过/失败
【发布时间】:2018-12-21 08:56:57
【问题描述】:

我是 Python 的新手,目前正在做一个小型测试用例分配,我要在其中找到字典键并将其匹配到一个小文本文件,并查看这些键是否存在于文本文件中。

字典如下:

dict = {"描述,翻译": "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".

使用我拥有的当前代码(并且我被卡住了),运行多次“测试通过”和“测试失败”返回的代码,这表明键在每一行上迭代多次并返回结果对于每个多次迭代。

我被困在两个方面:

  1. 将key拆分成列表后,如何按照“描述、翻译”、“单元”的顺序排列?
  2. 如何修改代码以保证返回一次结果为“Test pass”或“test fail”

理想情况下,结果应以以下格式返回:

理想的结果:

('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

请多多指教,谢谢!

【问题讨论】:

    标签: dictionary pattern-matching string-matching testcase


    【解决方案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")
    

    这个想法是创建一个单词列表,然后将它们全部打印出来 我正在使用format 操作,您可以找到有关如何使用它的指南here。并且if(words_found): 行检查列表是否为空。

    附加说明

    在这种情况下,您不需要它,但如果您只想解决第二点,您可以使用for else 语句,如in the docs 解释的那样

    4.4 break 和 continue 语句,以及循环上的 else 子句

    循环语句可能有一个 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")
    

    【讨论】:

    • 非常感谢您的建议!虽然代码有效,但是我有一个问题需要进一步建议:正如我所指出的,“描述,翻译”是与句子文本匹配的关键,“描述应显示XXX的翻译。 ”。由于句子中包含“描述”和“翻译”这两个词,它们应该返回“测试通过”,但返回“测试失败”结果。
    • 据我所见,python将“描述,翻译”识别为一键中的一大元素,但它们实际上是一键中的两个单独的元素(单词)。您发布的“word”功能似乎与此问题不兼容,也许还有其他语法(除了“word”)可以在代码中使用吗?再次为您提供好意的建议,谢谢!
    • 是的,对不起,我没有从问题中得到答案,正在编辑它。
    • 嗨,早上好!我已经尝试了您添加的修改代码--
    • for word in list: description, translation = word.split(",") # 如果单词匹配则添加到列表 words_found if description in line: words_found.append(description)跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2020-12-03
    • 2020-01-07
    • 2011-11-20
    • 1970-01-01
    • 2017-10-15
    相关资源
    最近更新 更多