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?
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'))
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?
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'))