I have a list of prepositions in a txt file. I am creating a function such that it will extract the word following the prepositions from a string. Since there are many prepositions , it would not be feasible to directly put them into re.compile . So i am using a txt file. Here's my code :
with open("Input.txt"):
words = "|".join(line.rstrip() for line in open)
pattern = re.compile('{}\s(\w+|\d+\w+)\s\w+'.format(words))
where {} would represent match of preps ,whereas \s is a space followed by a word or a combination of digits and words like 20th cross and so on. The error i'm getting is
TypeError Traceback (most recent call last)
<ipython-input-43-0aed517ef1ba> in <module>()
1 with open("Input.txt"):
----> 2 words = "|".join(line.rsplit() for line in open)
3 pattern = re.compile("{}\s(\w+|\d+\w+)\s\w+".format(words))
TypeError: 'builtin_function_or_method' object is not iterable
The Input.txt file has contents as ['near','above','towards'...] and so on.. how do i iterate over it??