vendredi 23 août 2019

Generate/Iterate text file through another text file

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

example: What my script does now

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

What I want it to do:

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




Aucun commentaire:

Enregistrer un commentaire