Reading two text files line by line simultaneously
with open("textfile1") as textfile1, open("textfile2") as textfile2:
for x, y in izip(textfile1, textfile2):
x = x.strip()
y = y.strip()
print(f"{x}\t{y}")
In Python 2, replace built-in zip
with itertools.izip
:
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))
Read two textfile line by line simultaneously -java
Put the calls to nextLine
on both readers in the same loop:
String english = "/home/path-to-file/english";
String french = "/home/path-to-file/french";
BufferedReader enBr = new BufferedReader(new FileReader(english));
BufferedReader frBr = new BufferedReader(new FileReader(french));
while (true) {
String partOne = enBr.readLine();
String partTwo = frBr.readLine();
if (partOne == null || partTwo == null)
break;
System.out.println(partOne + "\t" + partTwo);
}
Read two lines at a time from a txt file
you can iterate over the file using enumerate to get line numbers, and simply store even number lines in a temp variable, and move on to the next odd number line. On odd number lines you can then have access to the previous line as well as the current line.
with open('somefile.txt', 'r') as f:
lastline = ''
for line_no, line in enumerate(f):
if line_no % 2 == 0: #even number lines (0, 2, 4 ...) go to `lastline`
lastline = line
continue #jump back to the loop for the next line
print("here's two lines for ya")
print(lastline)
print(line)
How to read two files in parallel with python?
Use zip
over the files which are already generators:
for la, lb in zip(file_a, file_b):
...
How can two text files be read in parallel by a batch file?
The trick is to use STDIN redirection <
(see also this site) using undefined handles (3
to 9
) for the entire code block for file reading, the command set /P
in the block to actually read a line and 0<&
to redirect the undefined handles back to STDIN for set /P
, thus for the respective line to read.
Here is an example how it works:
Supposing there are the following two text files names.txt
...:
Black
Blue
Green
Aqua
Red
Purple
Yellow
White
Grey
Brown
...and values.txt
...:
0
1
2
3
4
5
6
7
...and the goal is to combine them line by line to achieve this file, names=values.txt
...:
Black=0
Blue=1
Green=2
Aqua=3
Red=4
Purple=5
Yellow=6
White=7
...the following code accomplishes that (see all the explanatory comments, rem
):
@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem // Define constants here:
set "FILE1=names.txt"
set "FILE2=values.txt"
set "RET=names=values.txt" & rem // (none to output to console)
if not defined RET set "RET=con"
rem /* Count number of lines of 1st file (2nd file is not checked);
rem this is necessary to know when to stop reading: */
for /F %%C in ('^< "%FILE1%" find /C /V ""') do set "NUM1=%%C"
rem /* Here input redirection is used, each file gets its individual
rem (undefined) handle (that is not used by the system) which is later
rem redirected to handle `0`, `STDIN`, in the parenthesised block;
rem so the 1st file data stream is redirected to handle `4` and the
rem 2nd file to handle `3`; within the block, as soon as a line is read
rem by `set /P` from a data stream, the respective handle is redirected
rem back to `0`, `STDIN`, where `set /P` expects its input data: */
4< "%FILE1%" 3< "%FILE2%" > "%RET%" (
rem // Loop through the number of lines of the 1st file:
for /L %%I in (1,1,%NUM1%) do (
set "LINE1=" & rem /* (clear variable to maintain empty lines;
rem `set /P` does not change variable value
rem in case nothing is entered/redirected) */
rem // Change handle of 1st file back to `STDIN` and read line:
0<&4 set /P "LINE1="
set "LINE2=" & rem // (clear variable to maintain empty lines)
rem // Change handle of 2nd file back to `STDIN` and read line:
0<&3 set /P "LINE2="
rem /* Return combined pair of lines (only if line of 2nd file is
rem not empty as `set /P` sets `ErrorLevel` on empty input): */
if not ErrorLevel 1 echo(!LINE1!=!LINE2!
)
)
endlocal
exit /B
Related Topics
Cannot List Ftp Directory Using Ftplib - But Ftp Client Works
Can Multiprocessing Process Class Be Run from Idle
Why Can't I Change Only a Single Element in a Nested List in Python
Unicodeencodeerror: 'Ascii' Codec Can't Encode Character '\Xe9' - -When Using Urlib.Request Python3
Using a Where _ in _ Statement
How Do Chained Comparisons in Python Actually Work
How to Extract Info Within a #Shadow-Root (Open) Using Selenium Python
Convert Python Strings into Floats Explicitly Using the Comma or the Point as Separators
Import CSV with Different Number of Columns Per Row Using Pandas
How to Make a Python Script Executable
How to Implement Band-Pass Butterworth Filter with Scipy.Signal.Butter
How to Install Pip for Python 2.6
How to Convert a Password into Asterisks While It Is Being Entered
Replace Column Values Based on Another Dataframe Python Pandas - Better Way