newbie question concerning formatted output

T

Thomas Liesner

Hello all,

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?

TIA,
../Tom
 
F

Fredrik Lundh

Thomas said:
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>
 
D

Dave Hansen

On Tue, 29 Nov 2005 17:40:08 GMT in comp.lang.python, Thomas Liesner
So instead of:

3905
3009
0000 [...]

i'd like to have:

3905 3009 0000 [...]

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?

Here's an obvious, if naive, implementation:

data = imp.read().split()
for i in range(0,len(data),3):
tl = data[i:i+3]
for d in tl:
print d,
print
inp.close()

HTH,
-=Dave
 
D

Dennis Benzinger

Thomas said:
[...]
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
 
W

wittempj

You can try something like:
#!/usr/bin/python
import sys
inp = open("xyplan.nobreaks")
data = [ i.strip() for i in inp if i ]
while data:
print ' '.join(data[0:3])
del data[0:3]
 
P

Patrick Down

a = [str(i) for i in range(0,17)].... print " ".join(a[i:i+3])
....
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
15 16
 
M

Micah Elliott

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

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

Right -- if the data is that regular then every 15th item is the
split-point. A variation on this theme then is:

for i in range(0, len(data), 15):
print data[i:i+15]
 
D

dwelch

Thomas said:
Hello all,

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?

TIA,
./Tom


Might be more than you need, but I've employed "TextFormatter"
(http://www.faqts.com/knowledge_base/view.phtml/aid/4517) to good effect.

-Don
 
S

Scott David Daniels

Thomas said:
... i want to print always three of them in a single line and
> after that a linebreak.

How about:

def emit(aFile):
for line in aFile:
for word in line.split():
yield word

f = open('xyplan.nobreaks', 'r')
gen = iter(emit(f)).next
try:
while True:
for i in range(2):
print gen(),
print gen()
except StopIteration:
pass
f.close()
print

--Scott David Daniels
(e-mail address removed)
 
B

Bengt Richter

Hello all,

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?
Using StringIO(""" ... """) in place of open("xyplan.nobreaks","r"),
(BTW, "r" is the default, and can be left out)
... 3905
... 3009
... 0000
... 4508
... f504
... 0000
... 3707
... 5a07
... 0000
... """)) ... print '%s %s %s' % tup
...
3905 3009 0000
4508 f504 0000
3707 5a07 0000

Or if the zip(it, it) grouping idiom is not politically correct, you could use
itertool.izip instead, or groupby with a grouping function that returns changing
constants for each group of three, e.g.,
... 3905
... 3009
... 0000
... 4508
... f504
... 0000
... 3707
... 5a07
... 0000
... """)) ... print '%s %s %s' % tuple(g)
...
3905 3009 0000
4508 f504 0000
3707 5a07 0000

Regards,
Bengt Richter
 
B

Bengt Richter

This is the codesnippet i am using:
Sorry, I made no comment on your actual code. Some follows.
#!/usr/bin/python

import string
I'm not seeing the need for importing string
inp = open("xyplan.nobreaks","r")
inp = open("xyplan.nobreaks") # defaults to "r"
data = inp.read()
for words in data.split():
At this point words would have to be a group of three successive words,
joined with a single space separator. So we can still use your loop
and only print with newline on the last of every group of three. So
we can use a counter to tell where in the cycle we are, e.g., combining
read() and split() and enumerating the split sequence, (untested)

for i, words in enumerate(inp.read().split()):
if i%3 < 2:
print words, # trailing comma makes a space separator if next print continues line output
else: # do as before and print final word with newline
print words
inp.close()

Any hints?

Upgrade if you don't have 2.4 ;-)
Then you can do a oneliner with a single print ;-)

Set up the data file example:
... 3905
... 3009
... 0000
... 4508
... f504
... 0000
... 3707
... 5a07
... 0000
... """)

Show it as is:
----
3905
3009
0000
4508
f504
0000
3707
5a07
0000
----

Now the one-liner, FWIW ;-)
>>> print '\n'.join(' '.join(z) for it in [(ln.strip() for ln in open('xyplan.nobreaks'))] for z in (zip(it, it, it) ))
3905 3009 0000
4508 f504 0000
3707 5a07 0000

Regards,
Bengt Richter
 
T

Thomas Liesner

Hi all,

thanks for all your answers. Is see that there are - as ususal - several
ways to accomplish this. I decided to go for the way Frederik suggested,
because it looked as the most straight forward method for that kind
of data.

Thanks again,
../Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top