Reputation: 55
I read text file like
awsm awesome
gr8 great
as dictionary i need to replace acronym words with it's correct one in dictionary i used this code but it give me error " replace() argument 2 must be str, not list" code :
def acronym(text):
with open('acronym.txt') as fin:
rows= (line.split('\t') for line in fin )
d = { row[0]:row[1:] for row in rows }
acronym_words = []
words = word_tokenize(text)
for word in words:
for candidate_replacement in d:
if candidate_replacement in word:
word = word.replace(candidate_replacement, d[candidate_replacement])
acronym_words.append(word)
acronym_sentence = " ".join(acronym_words)
return acronym_sentence
acronym(" we are awsm and gr8") # to test function
Upvotes: 0
Views: 39
Reputation: 36033
In:
d = { row[0]:row[1:] for row in rows }
row[1:]
is a slice which produces a list. Just use row[1]
since the dictionary values should be a string representing a single word, not a list.
Upvotes: 2