Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python, i have a .txt file that has rows of numbers. For example: I need to import this file above .txt file and sort

In python, i have a .txt file that has rows of numbers. For example:

image text in transcribed

I need to import this file above .txt file and sort the integers in each row and then output the sorted row into a .txt file. The problem I have is that the output file needs to match the exact format as above, but it is currently in string format which is incorrect. I don't know how to change it to match the above. This is my incorrectly formatted output:

image text in transcribed

This is my driver code (the very end is where I write the output to the file):

image text in transcribed

1 2 3 4 14 192511 812345612 50 -537-2 1234 PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL 1: vim [[2, 5, 11, 19], [1, 1, 2, 2, 3, 4, 5, 6], [-5, -2, 0, 3, 7], [2, 3, 4]] # main method if name _main__': # open, read and close file file = open ("datafile.txt", 'r') lines = file. readlines () file.close() # a array to store 2D array a = [] # fill array a from value in file for i in lines: print("New Line", i) row = [] for j in i.strip().split() [1:] : row.append(int(j)) a.append( row) # output array a print("This is array:", a) # outputs print("Given array is") printList(a) # sort each row using merge sort for i in a: mergeSort(i) # output print("Sorted array is:") printList(a) f = open("output.txt", "W") f.write(str(a)) f.close()]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions