Questions Create a list called emotions that contains the first word of every line in emotion_words.txt.
emotions=[]
with open("emotion_words.txt","r+") as f:
for line in f:
emotions.append(line.strip()[0])
Any Suggestions on where I have gone wrong... I know it has something to do with that last line
As suggested in the comments you need to read
the content of the file and then split the sentence using the split
method of strings
like this:
emotions=[]
with open("emotion_words.txt","r+") as f:
for line in f.readlines():
emotions.append(line.split()[0].strip())
I assume you mean
emotions.append(line.split()[0].strip())
otherwise you're saving the first letter of each line, not the first word.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.