How does this code not sot my data from a file?

Question:

f=open("classa2.txt", "r")
data=f.read()
sorted(data)
print(data)

This file is mean to sort my data but it does nothing, any ideas how to fix it?

Asked By: jim bob

||

Answers:

sorted is not in-place. It will only return a sorted list. You will have to assign it back

data = sorted(data)

This will overwrite the contents of data after sorting.

f=open("classa2.txt", "r")
data=f.readlines()
data=sorted(data)
print(data)

To print the contents of a list, you have to do

for i in data:
    print(i.strip('n'))
Answered By: Bhargav Rao
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.