How to join 2 lines from the output and print?

Viewed 158

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.

3 Answers

Try this :

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]:
        line1 = line1 + ';'
        line2 = line2.split(' ')
        line = ''
        for x in range(1,len(line2)):
            line = line + line2[x] + ' '
        line = line[:-1] + '.' 
        print(" ".join([line1, line]))

change print line with this:

with open('new.txt' , 'a+') as file:
    file.write(" ".join([line1, line]) + '\n')

This code add output in txt file only one time :

temp_list = ''
with open('temp.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]:
        line1 = line1 + ';'
        line2 = line2.split(' ')
        line = ''
        for x in range(1,len(line2)):
            line = line + line2[x] + ' '
        line = line[:-1] + '.' 
        temp_list += " ".join([line1, line]) + '\n'

with open('new.txt' , 'w') as file:
    file.write(temp_list)

You can use write to save data to text file. Please see below:

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]:
    line = " ".join([line1, line2])        
    print(line)
    with open('outfile.txt', 'a') as of:
      of.write(line + '\n')

I think what you need is to group and iterate; once do that, you will able to write it to a file easily. Something like this:

data = """\
Jack loves dancing
Jack is a dancer
Tony likes candy
Tony ate candy\
"""

def grouped(iterable, n):
    return zip(*[iter(iterable)]*n)

for x, y in grouped(data.splitlines(), 2):
    print(x + " ; " + y)

Refer: Iterating over every two elements in a list

Related