python - Appending multiple list index into a single list then using join to print them -
hello have script script should take 2 files split them 2 lists after add them in 1 list , write content in 1 file problem 1 of file might have list length on 1+ index of them 5 of them 10 need w.e length of first , second list add them single list , write them in single file without [] , ' between data thank here script
import re f = open('firsttext.txt', 'r') d = open('second.txt', 'r') w = open('combination.txt','w') s="" filtred=[] mlines=f.readlines() wlines=d.readlines() line in wlines: wspl=line.split() line2 in mlines: mspl=line2.split() if ((mspl[0]).lower()==(wspl[0])): wspl.append(mspl[1]) print (wspl) s="\t".join(wspl)+"\n" if s not in filtred: filtred.append(s) break x in filtred: w.write(x) f.close() d.close() w.close()
get content of each file first, join them.
from itertools import izip open('file1.txt') f1: lines1 = f1.readlines() open('file2.txt') f2: lines2 = f2.readlines() open('combinations.txt', 'w+') f3: i, j in izip(lines1, lines2): f3.write(i + ' ' + j.split(none, 1)[-1]))
Comments
Post a Comment