Reputation: 11
So I am trying to sort a list of emails to see how many are sent by each email but I keep getting:
filename = input("Enter File Name ")
file = open(filename)
lst = list()
for line in file :
if line.startswith('From: '):
y = line.split('From: ')[1]
y.sort()
print(y)
----> 7 y.sort()
8 print(y)
AttributeError: 'str' object has no attribute 'sort'
Upvotes: 0
Views: 212
Reputation: 1194
You need to append each line to your list, then sort the list. y.sort()
is trying to sort an individual line; the error happens because y
is a string, and they don't have a sort()
method.
filename = input("Enter File Name ")
file = open(filename)
lst = list()
for line in file:
if line.startswith('From: '):
lst.append(line.split('From: ')[1])
lst.sort()
print(lst)
Upvotes: 5