Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading two text files line by line simultaneously

I have two text files in two different languages and they are aligned line by line. I.e. the first line in textfile1 corresponds to the first line in textfile2, and so on and so forth.

Is there a way to read both file line-by-line simultaneously?

Below is a sample of how the files should look like, imagine the number of lines per file is around 1,000,000.

textfile1:

This is a the first line in English This is a the 2nd line in English This is a the third line in English 

textfile2:

C'est la première ligne en Français C'est la deuxième ligne en Français C'est la troisième ligne en Français 

desired output

This is a the first line in English\tC'est la première ligne en Français This is a the 2nd line in English\tC'est la deuxième ligne en Français This is a the third line in English\tC'est la troisième ligne en Français 

There is a Java version of this Read two textfile line by line simultaneously -java, but Python doesn't use bufferedreader that reads line by line. So how would it be done?

like image 467
alvas Avatar asked Jul 02 '12 13:07

alvas


People also ask

How do I read two files at once in Python?

Steps Needed Steps used to open multiple files together in Python : Both the files are opened with open() method using different names for each. The contents of the files can be accessed using readline() method. Different read/write operations can be performed over the contents of these files.

Can help you to read one line each time from the file?

The readline() method helps to read just one line at a time, and it returns the first line from the file given. We will make use of readline() to read all the lines from the file given. To read all the lines from a given file, you can make use of Python readlines() function.

How do I read two files at once in Perl?

#!/usr/bin/perl use strict; use warnings; sub read_file_line { my $fh = shift; if ($fh and my $line = <$fh>) { chomp $line; return [ split(/\t/, $line) ]; } return; } sub compute { # do something with the 2 values } open(my $f1, "file1"); open(my $f2, "file2"); my $pair1 = read_file_line($f1); my $pair2 = ...


2 Answers

from itertools import izip  with open("textfile1") as textfile1, open("textfile2") as textfile2:      for x, y in izip(textfile1, textfile2):         x = x.strip()         y = y.strip()         print("{0}\t{1}".format(x, y)) 

In Python 3, replace itertools.izip with the built-in zip.

like image 80
Fred Foo Avatar answered Oct 07 '22 18:10

Fred Foo


with open(file1) as f1, open(fil2) as f2:   for x, y in zip(f1, f2):      print("{0}\t{1}".format(x.strip(), y.strip())) 

output:

This is a the first line in English C'est la première ligne en Français This is a the 2nd line in English   C'est la deuxième ligne en Français This is a the third line in English C'est la troisième ligne en Français 
like image 22
Ashwini Chaudhary Avatar answered Oct 07 '22 17:10

Ashwini Chaudhary