newbie question concerning formatted output

Dennis Benzinger Dennis.Benzinger at gmx.net
Tue Nov 29 13:29:38 EST 2005


Thomas Liesner schrieb:
> [...]
> 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.
> [...]


words = ["3905", "3009", "0000", "4508", "f504", "0000", "3707", "5a07", 
"0000", "XXXX"]

VALUES_PER_LINE = 3

# Simple version
for index, word in enumerate(words):
     print word,  # Trailing comma to suppress newline
     if (index + 1) % VALUES_PER_LINE == 0:
         print

print
print

# Trickier version
ranges =[]
for i in range(VALUES_PER_LINE):
     ranges.append(words[i::VALUES_PER_LINE])

for triple in map(None, *ranges):
     print " ".join(str(value) for value in triple if value is not None)


Hmm, the second solution seems to be a bit too tricky...

Bye,
Dennis



More information about the Python-list mailing list