You opened the same file twice. The second one is under a loop.
You have a condition. According your data, only line U or line W can have your if condition.
Next issue is:
What happen if your if condition matched, You open a file name
newdata.txt with
w, in other word for every match it will crate an empty file destroying the previous file. i.e. If your match matches and even if there is more than one match, you will get only one output. But, according to your next commands, that's not gonna happen either.
Next>
You are reading
tweets2009-10.txt as fin. You read the same file
tweets2009-10.txt in the upper loop.
I will stop here.
Your whole logic is wrong.
The suggested logic would be:
open output to write
while(more lines in file)
read next 3 lines;
if line 2 or line 3 contains preferred words; then
format 3 lines as a single line;
write single line to output file