I wold like to expand acronyms in text file (e.g fyi--->for your information). I started by reading the acronyms file and store them in a dictionary as keys and values. Next, search each line in the text file for acronym(s) and expand them into complete phrase. After checking this post , I was able to read and create acronyms dictionary, but I have an issue with the replacement. The output file that should include the correct text is empty. here is my code and thanks in advance:
d = {}
with open('acronym.txt', 'r') as acronym:
for line in acronym:
d[line.split(None, 1)[0]] = (line.split(None, 1)[1]).strip()
ntweetsfile= open('newTweets.txt', 'w')
import fileinput
for line in fileinput.input('tweets1k.txt', inplace=True):
line = line.rstrip()
if not line:
continue
for key in d:
if str(key) in line:
line = line.replace(str(key), d[key])
ntweetsfile.write(str(line)+'\n')
else:
ntweetsfile.write(str(line)+'\n')
ntweetsfile.close()