I looking for help. I managed to add row_id to my file using the following code and it`s working fine.
infile = open(inputfile, 'r')
outfile = open(outputfile, 'w')
for i, line in enumerate(infile):
#write header
row_sec = ("00000" + str(i))[-5:]
row_nr = max(row_sec)
outfile.write(row_sec + "Company" + column[0] + column[1] + row_nr + "\n")
outfile.close()
infile.close()
results:
The problem is that I need to put nr of records in header line (in this case it would be 00037)
when I use
records = max(i)
or
records = max(row_id)
I get error TypeError: 'int' object is not iterable
I read about this error and understand it but still can`t figure out how to get rid of it. Is there a clever and simple way to fix my code?
You can delay writing and grab the variables from the loop:
buffer = []
for i, line in enumerate(infile):
# create line content
buffer.append('%5d' % i + " Company " + line + "\n")
# i is still set to the last value
# write header
outfile.write('%5d' % i + " Elements\n")
for line in buffer:
outfield.write(line)
This will write something like
00037 Elements
00000 Company ACME
00001 Company EMCA
...
00037 Company MECA
Note that enumerate
takes an optional parameter start
if you need to set the first index.
If your file is larger than available memory, you can read it twice instead of buffering:
# read once to get number of entries
with open(inputfile, 'r') as infile:
companies = sum(1 for line in infile)
infile = open(inputfile, 'r')
outfile = open(outputfile, 'w')
# write header
outfile.write('%5d' % companies + " Elements\n")
for i, line in enumerate(infile):
row_sec = ("00000" + str(i))[-5:]