[Python] Why is my Python parser method returning empty strings?

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to write a simple parser method in Python. It takes in a filename pointing to a file of a certain format. An example of this format is below:

    File:
    .type = INFILE
    .fmt = CFP_INPUTFILE_FMT_2
    Data:
    .cases
    .given = True
    .numCases = 2
    .case
    .numlines = 2
    .line
    .value = 3
    .value = 3
    .line
    .value = 3
    .value = 3
    .case
    .line
    .value = 3
    .value = 3
    .line
    .value = 3
    .value = 3


    The parser reads this file and outputs the raw data to a terminal via subprocess.run() and prints the output. The data printed for the file should look like this:

    2
    33
    33
    33
    33


    The method itself looks something like this:

    @classmethod
    def input_file_fmt_1_to_input(cls, inputfile: str):
    with open('temp.txt', 'w') as temp:
    try:
    with open(inputfile, 'r') as file:
    case = 0
    line = 0
    case1line1 = ''
    case1line2 = ''
    case2line1 = ''
    case2line2 = ''
    for lne in file.readlines():
    cleanline = lne.lstrip().rstrip()
    if cleanline.startswith('.numCases'):
    split = cleanline.split(' ')
    value = str(split[2]) + '\n'
    temp.write(value)
    elif cleanline.startswith('.case'):
    case += 1
    elif cleanline.startswith('.line'):
    line += 1
    elif cleanline.startswith('.value') and case == 1 and line == 1:
    case1line1 = case1line1 + str(cleanline.split(' ')[2]) + ' '
    elif cleanline.startswith('.value') and case == 1 and line == 2:
    case1line2 = case1line2 + str(cleanline.split(' ')[2]) + ' '
    elif cleanline.startswith('.value') and case == 2 and line == 1:
    case2line1 = case2line1 + str(cleanline.split(' ')[2]) + ' '
    elif cleanline.startswith('.value') and case == 2 and line == 2:
    case2line2 = case2line2 + str(cleanline.split(' ')[2]) + ' '
    else:
    pass

    if case1line1 != '':
    temp.write(case1line1)
    temp.write('\n')
    elif case1line2 != '':
    temp.write(case1line2)
    temp.write('\n')
    elif case1line3 != '':
    temp.write(case2line1)
    temp.write('\n')
    elif case1line4 != '':
    temp.write(case2line2)
    temp.write('\n')

    result = run('cat temp.txt', shell=True, capture_output=True)
    print(result.stdout)

    except FileNotFoundError as e:
    raise CfpRuntimeError from e


    I've written a pytest test for this method, which looks like this:

    def test_input_file_fmt_1_to_input_test(capsys):
    cfp_testcontext.InputParser.input_file_fmt_1_to_input('../../TEST/modules_test/testfile.cfpin')
    captured = capsys.readouterr()
    assert captured.out == '2\n3 3 \n3 3 \n3 3 \n3 3 '


    When I run the test, I get an assertion error:

    assert "b''\nb''\nb''\nb''\n" == "2\n3 3 \n3 3 \n3 3 \n3 3 "

    1. Where are the strings are being transformed into byte strings?
    2. Why does my result contain only empty strings?

    I have tried writing a a fake method and fake test using capsys to see if the output was empty, and if it was a byte string. The output was a byte string, so I believe that the conversion is happening from within pytest. However, the function, which wrote 'hello' to the console, worked perfectly, so I am pretty sure there is something wrong with my logic.

    Continue reading...

Compartilhe esta Página