python - strip '\n' from lines of multiple simultaneously opened files -
i need open small variable number of files, each of has same number of lines. need return tuple of lines in each. code returns tuple, each element retains '\n'; how strip before gets packed tuple?
this code far
files = ['file1', 'file2', 'file3'] fds = [ open(file) file in files ] # gets file open @ same time read_fds = izip( *fds ) tpl in read_fds: print tpl # become 'yield' stmt once '\n' sorted fd in fds: fd.close()
i have test set of 3 files, each 5 lines, each indicates file number line number. code prints accurate record of these test files.
('f1ln1\n', 'f2ln1\n', 'f3ln1\tthis\tthat\n') ('f1ln2\n', 'f2ln2\n', 'f3ln2\tthis\tthat\n') ('f1ln3\n', 'f2ln3\n', 'f3ln3\tthis\tthat\n') ('f1ln4\n', 'f2ln4\n', 'f3ln4\tthis\tthat\n') ('f1ln5\n', 'f2ln5\n', 'f3ln5\tthis\tthat\n')
so far good, how strip() '\n' each line before gets packed tuple?
i know there's answer out there! looking fwd suggestions. thank & have great day.
you can use slicing of string
for tpl in read_fds: list_stripped = [] s in tpl: list_stripped.append(s[:-1]) print tuple(list_stripped)
Comments
Post a Comment