Open the sentimentvalues.csv file with open('sentimentvalues.csv', 'r') as file: Iterate…
- User
Open the sentiment_values.csv file
with open('sentiment_values.csv', 'r') as file: # Iterate through each line in the file for line in file: # Split each line to create a list containing the word and its sentiment score word, sentiment_value = line.strip().split(',') sentiment_value_dictionary[line[0]] = line[1]
However, the dictionary that you will be creating will much longer. In fact, it will contain 6,271 entries! You will need to use your new accessing file skills to do this. You can think of your task in two parts:
Split each line to create a list that contains the word and its sentiment score. As a reminder, split() will separate a String into a list based on the parameter. Take a look at the sentiment values.csv file. What separates each word from its sentiment value?
Add the word-value pair to the sentiment_value_dictionary. Check out the Docs tab or this Dictionaries in Python tutorial if you need a refresher on how to do this.
Once your done, print the final dictionary to the console to make sure that it works!
- Assistant
sentiment_value_dictionary = {}
with open('sentiment_values.csv', 'r') as file: for line in file: word, sentiment_value = line.strip().split(',') sentiment_value_dictionary[word] = float(sentiment_value)
print(sentiment_value_dictionary)
- User
indented
- Assistant
sentiment_value_dictionary = {}
with open('sentiment_values.csv', 'r') as file: for line in file: word, sentiment_value = line.strip().split(',') sentiment_value_dictionary[word] = float(sentiment_value)
codeprint(sentiment_value_dictionary) - User
fix dents
- Assistant
sentiment_value_dictionary = {}
with open('sentiment_values.csv', 'r') as file: for line in file: word, sentiment_value = line.strip().split(',') sentiment_value_dictionary[word] = float(sentiment_value)
print(sentiment_value_dictionary)
- User
indented block
- Assistantpython
sentiment_value_dictionary = {} with open('sentiment_values.csv', 'r') as file: for line in file: word, sentiment_value = line.strip().split(',') sentiment_value_dictionary[word] = float(sentiment_value) print(sentiment_value_dictionary)