Reputation: 13
So I've been following along some of the projects from the 'Big Book of Small Python Projects' by Al Sweigart and ive been attempting to follow along one of the projects, however Ive been stuck on this segment of code for a while now and was wondering if anyone could help. Anytime i run the code I keep getting an error on this line "WORDS[i] = WORDS[i].strip().upper()". It says "'str' object does not support item assignment" and I am unsure how to fix. Any help is greatly appreciated.
# Load the WORDS list from the text file
with open('sevenletterwords.txt') as wordListFile:
WORDS = wordListFile.readline()
for i in range(len(WORDS)):
# Convert each word to uppercase and remove the trailing newline
WORDS[i] = WORDS[i].strip().upper()
Upvotes: 0
Views: 168
Reputation: 16081
You need to use readlines
:
with open('test.csv') as wordListFile:
WORDS = wordListFile.readlines() # updated here
for i in range(len(WORDS)):
# Convert each word to uppercase and remove the trailing newline
WORDS[i] = WORDS[i].strip().upper()
Here is an approach that uses a lambda
function:
with open('test.csv') as wordListFile:
WORDS = wordListFile.readlines()
WORDS = list(map(lambda x: x.strip().upper(), WORDS))
print(WORDS)
Upvotes: 2