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

How to write a statement and an array to individual lines on a text file? Python

a = str(mergeSort(lines))
nlines = ['Sorted Numbers',a]
with open('SortedNumbers.txt', 'w') as f:
    f.writelines(nlines)enter code here

mergeSort(lines) is my array of a few numbers. With this code above, the text file it creates does everything I want except all of it is on one line. How do I get it so the text file it produces will be

Sorted Numbers
1
3
5
8
10

>Solution :

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

a = "\n".join(mergeSort(lines));

Should put a newline after each of the numbers of your array.

It should print properly afterwards

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