I have values (this are the sentiments: "negative", "neutral", "positive") from a text file. The keys are the adjectives. I want to produce a dictionary with the adjectives and new values, namely -1, 0, 1.
This is what I produced so far:
dictionary = {} #creating an empty dictionary
infile = open('adjective_sentiment.txt', 'r')
for line in infile:
key, value = line.split()
dictionary[key] = value
print(dictionary)
How can I access the values and replace them with the numbers?
You could do it like this:
D = {'negative': -1, 'neutral': 0, 'positive': 1}
dictionary = dict()
with open('adjective_sentiment.txt') as infile:
for line in infile:
a, s = line.split()
dictionary[a] = D[s]
print(dictionary)
Of course, this could go horribly wrong if the file is not formatted exactly as stated
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.