I read the .txt file and then picked a few words from in different sentences and after reading the text and printed them. Now I want to take that output as input and then join line1+line2, line3+line4, .....). And also when I am printing the output I don't want the first word from the second line (which means from the even number of lines as in line 2, line 4 ....) and replace that with ';'. Here is the output that I printed from the text file.
Jack love Dancing
Jack is a dancer
Tony likes candy
Tony ate candy
Jack love Dancing
Jack is a dancer
Tony likes candy
Tony ate candy
Now I want to store that in .txt file and print as
Jack love Dancing ; is a dancer.
Tony likes candy ; ate candy.
Jack love Dancing ;is a dancer.
Tony likes candy ; ate candy.
I have gone through how to join two lines from the text file, but I am not sure how to save the output from print statement to .txt file (in the program) and remove the first word by replacing with ';' Code
with open('file.txt') as f:
out = [x for x in f.read().split("\n") if x.strip() != ""]
for line1, line2 in list(zip(out, out[1:]))[::2]:
print(" ".join([line1, line2]))
output for the above code
Jack love Dancing Jack is a dancer
Tony likes candy Tony ate candy
Jack love Dancing Jack is a dancer
Tony likes candy Tony ate candy
Any suggestions would be appreciated. Thanks in advance.