[Tutor] Printing to file on one line

Jeff Shannon jeff at ccvcorp.com
Tue Sep 23 13:57:48 EDT 2003


Peter Brown wrote:

>>aba.write('0' + (' ' * 17) + string.zfill(d_batch, 2) + 'MET' + (' ' *
>>7) + conf_name.ljust(26) + conf_apca[:6] + conf_desc.ljust(12) +
>>str(smdate) + '\r\n')
>>a = '1' + a8f + a9f + ' ' + a3 + a4f + a5f + a6f + a1f + a2f + a10 +
>>'00000000\r\n' 
>>aba.write(a)
>>The first write goes over two lines - I want it all on one line:
>>
>>0                 32MET       PB Exports                 233381Payments 
>> 220903^M

It's hard to be sure, but this *looks* to me like it's a case of 
normal line-wrapping, rather than that it's been written to the file 
as separate lines.

One thing that might help is to break up your string-building and your 
write statement into several parts.  That will make it easier to 
inspect what you're writing out to the file.

Also, if you use string formatting with the % operator, that might 
make things a little clearer than your current formatting.  I've 
dummied up something that seems to follow the same formatting rules 
that you're using, printing to the screen instead of writing to a file 
(but once the string is constructed, either should be the same).

 >>> def write_debit(aba_file, d_batch, conf_name, conf_apca, 
conf_desc, smdate):
... 	batchname = '%02dMET' % d_batch
... 	format = '%-18s%-12s%-26s%-6s%-12s%s\r\n'
... 	output = format % ('0', batchname, conf_name, conf_apca[:6], 
conf_desc, str(smdate))
... 	print "Outputting %d bytes to file" % len(output)
... 	#aba_file.write(output)
... 	print output
...
 >>> write_debit(aba_file, batch, name, apca, desc, smdate)
Outputting 82 bytes to file
0                 32MET       PB Exports                233381Payments 
    220903

 >>>

Notice that this string is 82 characters long -- most mailers wrap at 
76 characters (as mine is doing), but on my screen this started as a 
single line.  (That is to say, it's my mailer that's wrapping it, not 
Python.)  Most editors will wrap long lines, too.  So I do believe 
that your "two lines" problem is just a matter of how you're viewing it.

As for why the second line you give doesn't show the problem, it's 
hard to say without seeing sample output or values for all those 
variables, but I expect that that line is just a couple characters 
shorter, so it doesn't hit your line-wrap point.  That line would also 
be more clear using string formatting instead of addition.

As a minor additional point, string formatting is also quite a bit 
more efficient than repeated string addition.  The performance hit is 
probably not significant unless you're doing it in a long loop, but 
keep in mind that every addition creates a temporary string, and most 
of those are thrown away -- for example, your second line generates 11 
more temporary strings than an equivalent formatting-based algorithm. 
  If you're writing 100 lines to a file, that's over a thousand 
temporary strings that the interpreter has to allocate memory for, 
construct, and then throw away.  All that extra work can start to add 
up...

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list