Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

write a python code to fill the unique series of number in a specific location in an txt file

Help to write a code to fill the series of numbers in a specific location in an txt file.

For example: I am having two files inputfile1.txt and inputfile2.txt

below is my inputfile1.txt content which is having some order_numbers parameter.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<p name = "order_number"></p>
<p name = "task_number"></p>
<p name = "order_number"></p>
<p name = "order_number"></p>
<p name = "taks_number"></p>
<p name = "order_number"></p>
<p name = "task_number"></p>

and the inputfile2.txt is having some unique sequence numbers.

order_number
277
254
635
789

Please help me with the such python code which provides below output.txt file.

I am expecting below output:

<p name = "order_number">277</p>
<p name = "task_number"></p>
<p name = "order_number">254</p>
<p name = "order_number">635</p>
<p name = "taks_number"></p>
<p name = "order_number">789</p>
<p name = "task_number"></p>

Below is my code please check.

def main():
    with open('inputfile1.txt', 'r') as f1, open('inputfile2.txt', 'r') as f2:
        lines1 = f1.readlines()
        lines2 = f2.readlines()

    output_lines = []

    sequence_index = 0
    for line in lines1:
        if 'name = "order_number"' in line:
            output_lines.append(line.strip() + lines2[sequence_index].strip() + "</p>\n")
            sequence_index += 1
        else:
            output_lines.append(line)

    with open('output.txt', 'w') as output_file:
        output_file.writelines(output_lines)

if __name__ == "__main__":
    main()

but the output is not as expected.
my output:

<p name = "order_number"></p>277</p>
<p name = "task_number"></p>
<p name = "order_number"></p>254</p>
<p name = "order_number"></p>635</p>
<p name = "taks_number"></p>
<p name = "order_number"></p>789</p>
<p name = "task_number"></p>

I want below output:

<p name = "order_number">277</p>
<p name = "task_number"></p>
<p name = "order_number">254</p>
<p name = "order_number">635</p>
<p name = "taks_number"></p>
<p name = "order_number">789</p>
<p name = "task_number"></p>

>Solution :

I have reconstruct your idea on kaggle

I have updated some input (removing order_number on inputfile2.txt) .
The output is similiar your expected result.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading