samedi 24 août 2019

Reading 1 text file through another text file or scan each other in opposite directions, File 1 down, File 2 up

Using Python

I have 2 text files that opens side by side on the same line in terminal, but I want the file on the left to generate/iterate down the other file while the file on the right is stationary. I want to generate/iterate text file 1 through all of text file 2.

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}{1}".format(x, y))



example: What my script does now 

text file1 text file2         
  asdf9s     3kf44e
  wfb4rs     c4vb1b
  vl7rcs     orbds0
  cv96rv     medyt2
  cvrb5r     p5f4sd
  7dfxgd     

What I desire it to do:

textfile1 
 asdf9s
 wfb4rs
 vl7rcs
 cv96rv
 cvrb5r
 7dfxgd   
   ↓          ↑
          textfile2
           3kf44e  
           c4vb1b  
           orbds0  
           medyt2  
           p5f4sd  

textfile1 textfile2 
 asdf9s    3kf44e
 wfb4rs    c4vb1b
 vl7rcs    orbds0
 cv96rv    medyt2
 cvrb5r    p5f4sd
 7dfxgd      

textfile1        
 asdf9s
 wfb4rs
 vl7rcs
 cv96rv
 cvrb5r
 7dfxgd




Aucun commentaire:

Enregistrer un commentaire