[Tutor] Printing to file on one line

Jeff Shannon jeff at ccvcorp.com
Wed Sep 24 12:54:14 EDT 2003


Peter Brown wrote:
>   Jeff,
> 
> Thanks for your good work. The example you gave works well for Windows
> but not Linux.
> 
> The example I sent yesterday was from Redhat 9. The output below shows
> outputting 82 characters, but the line is still wrapped note '\n' on
> first line (after payments). How did that \n get there? My programme
> didn't include it, is it a bug in Python on Redhat 9 - Python 2.2.2?
> Secondly, the date has two spaces preceeding it, but no formatting to
> accomplish this?

>  print repr(open('/usr/TP/20030924.aba', 'rb').read(82))
> '0                 68PET       PA Exports                
> 239381Payments\n  240903\r\n'

Those spaces are part of the conf_desc column -- %-12s indicates that 
the string should be left-justified in a twelve-character-wide space.

As for the extra '\n', is there a chance that it's part of the string 
that you're passing in?  Where are you reading conf_desc from?  The 
most likely explanation I can see is that you're formatting in 
'Payments\n' rather than just 'Payments'.  You can test this by 
changing the formatting line, modifying conf_desc to be 
conf_desc.strip() -- that will remove all whitespace (including 
newlines) from the beginning and end of the string.

Indeed, the fact that there's only two spaces preceding the date 
suggests that the \n *is* part of conf_desc.  'Payments' is only nine 
characters, and if that was formatted into a 12-char wide field, there 
should be three spaces following.  However, 'Payments\n' is ten 
characters long, so that would only leave two spaces...

Jeff Shannon
Technician/Programmer
Credit International




More information about the Tutor mailing list