newbie question concerning formatted output

Fredrik Lundh fredrik at pythonware.com
Tue Nov 29 13:05:56 EST 2005


Thomas Liesner wrote:

> i am having some hard time to format the output of my small script. I am
> opening a file which containes just a very long string of hexdata
> seperated by spaces. Using split() i can split this string into single
> words and print them on stdout. So far so good. But i want to print always
> three of them in a single line and after that a linebreak.
>
> So instead of:
>
> 3905
> 3009
> 0000
> 4508
> f504
> 0000
> 3707
> 5a07
> 0000
> etc...
>
> i'd like to have:
>
> 3905 3009 0000
> 4508 f504 0000
> 3707 5a07 0000
> etc...
>
> This is the codesnippet i am using:
>
> #!/usr/bin/python
>
> import string
> inp = open("xyplan.nobreaks","r")
> data = inp.read()
> for words in data.split():
>         print words
> inp.close()
>
> Any hints?

how about

inp = open("xyplan.nobreaks","r")
data = inp.read()

import textwrap
for line in textwrap.wrap(data, 15):
    print line

</F>






More information about the Python-list mailing list