l am able to open and read a file but my problem is to sort the content in alphabetical order can someone help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

l am able to open and read a file but my problem is to sort the content in alphabetical order can someone help

15th Oct 2016, 7:13 PM
James T Togara
James T Togara - avatar
2 Answers
+ 6
Something like this: with open('filename.txt', 'r') as f: for line in sorted(f): print(line, end='')
15th Oct 2016, 8:51 PM
Yethrosh
Yethrosh - avatar
+ 1
You could open each line of the file into a list, and sort that list into alphabetical order: file = open ("filename. txt","r") list = file.readlines () list.sort () Hope this alternative way helps!
7th Jan 2017, 9:46 AM
Ethan
Ethan - avatar