The problem is that as of python 2.7.10, the fileinput module does not support being used as a context manager, i.e. the with
statement, so you have to handle closing the sequence yourself. The following should work:
f = fileinput.input(files=('cutflow_TTJets_1l.txt ', 'cutflow_TTJets_1l.txt '))
for line in f:
proc(line)
f.close()
Note that in recent versions of python 3, you can use this module as a context manager.
For the second part of the question, assuming that each file is similarly formatted with an equal number of data lines of the form xxxxxx & xxxxx
, one can make a table of the data from the second column of each data as follows:
Start with an empty list to be a table where the rows will be lists of second column entries from each file:
table = []
Now iterate over all lines in the fileinput
sequence, using the fileinput.isfirstline()
to check if we are at a new file and make a new row:
for line in f:
if fileinput.isfirstline():
row = []
table.append(row)
parts = line.split('&')
if len(parts) > 1:
row.append(parts[1].strip())
f.close()
Now table
will be the transpose of what you really want, which is each row containing the second column entries of a given line of each file. To transpose the list, one can use zip
and then loop over rows the transposed table, using the join
string method to print each row with a comma separator (or whatever separator you want):
for row in zip(*table):
print(', '.join(row))
__enter__
or__exit__
– Cleotildeclepewith contextlib.closing(fileinput.FileInpt(..)) as file:
on Python 2. – Milanmilanese