I am trying to replace certain words in file which has mutliplelines. Below is the code I wrote. Please note I am still learning python.
ParsedUnFormattedFile = io.open("test.txt", "r", encoding="utf-8", closefd=True).read()
remArticles = {' a ':'', ' the ':'', ' and ':'', ' an ':''}
for line in ParsedUnFormattedFile:
for i in remArticles.keys():
words = line.split()
ParsedReplacementFile = ParsedUnFormattedFile.replace(words,remArticles[i])
FormattedFileForIndexing = io.open("FormattedFileForIndexing.txt", "w", encoding="utf-8", closefd=True)
FormattedFileForIndexing.write(ParsedReplacementFile)
If I am replacing by directly reading a line, it replaces only 1 word out of all words. It's usually 'the' in my system.
So I wanted to split and look for ever word and then replace it. However I get below error:
line 14, in <module>
ParsedReplacementFile = ParsedUnFormattedFile.replace(words,remArticles[i])
TypeError: coercing to Unicode: need string or buffer, list found
How can I get this rectified?
Thanks