Unable To Sort Multiple Lines
My aim: to sort multiple lines in a text file using Python Method A sample file has been created and this is used to test the Python script Code: f = open('C:\\sample.txt', 'r') li
Solution 1:
You are printing list.sort
function.
try this
lines.sort()
printlines
list.sort
perform operation on same object. If you don't want to change original list, then you can create sorted list using sorted(list)
function.
Solution 2:
Try this
file_name = open('Sample.txt','rb')
new_list = list(file_name)[0].split(',')
new_list = sorted(new_list)
for items in new_list:
print items
Output:
aa
bb
cc
ff
gg
ii
If your file doesn't contain any ',' then your sample input is gg bb .. then try this
file_name = open('input1.txt','rb')
new_list = []
for items in file_name:
if items != '\n':
items = items.replace("\r\n", "")
new_list.append(items)
new_list = sorted(new_list)
for i in new_list:
print i
Output:
aa
bb
cc
ff
gg
ii
Post a Comment for "Unable To Sort Multiple Lines"