1

I got a question about redirect the screen output into a single file. Here is my code to print the screen output:

for O,x,y,z,M,n in coordinate:
    print(O,x,y,z,M,n)

And the screen output looks like:

O 0 0 0 ! 1
O 1 0 0 ! 2 
O 2 0 0 ! 3

So how can I redirect all the data into a single file and in the same format, just like the screen output. Because it will be mush faster to get all the data rather than waiting for the screen output to finish. I triedfor point in coordinate: file.write(' '.join(str(s) for s in point)) but output file became:

O 0 0 0 ! 0O 1 0 0 ! 1O 2 0 0 ! 2O 3 0 0 ! 3O 4 0 0 ! 4O 5 0 0 ! 5O 6 0 0 ! 6O
Jianli Cheng
  • 371
  • 1
  • 8
  • 17

4 Answers4

2

The easiest way is not to do it in Python, but let the OS do it for you. This works both in Linux and the Windows command prompt.

python myprog.py >output.txt
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • This is in my opinion the best way to handle this, unless of course the actual goal of your program is to write the output to files. Then it's probably better to handle it in code, otherwise properly running the code requires knowing that you have to run it like this. – kniteli Sep 17 '13 at 02:05
  • @kniteli My goal is to redirect all the screen output into a single file. I am a novice in python and I have been struggled in this problem all day. – Jianli Cheng Sep 17 '13 at 02:27
1

The function call file.write(*point) essentially takes each element inside the point list, and modifies the function call to look like this: file.write(p1, p2, p3, p4, ...).

However, file.write takes in only a single argument -- a string. That means you need to transform the point list into a string.

It will probably end up looking like this:

with open('substrate', 'w') as file:
    for point in coordinate:
        file.write(' '.join([str(p) for p in point])
smac89
  • 39,374
  • 15
  • 132
  • 179
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
1

Try

with open('substrate', 'wb') as file:
    file.write('\n'.join(' '.join(str(p) for p in point)) for point in coordinate)

If you are wondering why wb? See this question

If you want to go with Mark Ransom's answer, I believe this is how you do it in code:

from sys import stdout
stdout.write('\n'.join(' '.join(str(p) for p in point)) for point in coordinate)
Community
  • 1
  • 1
smac89
  • 39,374
  • 15
  • 132
  • 179
  • I tried your code. But I my output file only contains:s s ss s, etc. – Jianli Cheng Sep 16 '13 at 22:58
  • @JianliCheng OK, it seems you are using python 3. See update code – smac89 Sep 16 '13 at 23:01
  • The output file is still not I am looking for. In the output file, n jump to the 10, then 20,30,40,etc., not 1,2,3,4,5...Also, how can I make each line in the output file that is only occupied by one point, like: O 0 0 0 ! 1/n O 1 0 0! 2/n – Jianli Cheng Sep 17 '13 at 01:27
  • @JianliCheng Update your original post with the output you get in your file when you use this code – smac89 Sep 17 '13 at 01:43
  • @JianliCheng I see where your problem is, I will update my post in a minute – smac89 Sep 17 '13 at 03:26
0

Not exactly sure what you're looking for, so I'm going two routes.

for coordset in coordinate:
    for point in coordset:
        file.write(point)

Or, if you want formatting, you can use a formatting string.

for coordset in coordinate:
    file.write('%s,%s,%s,%s,%s,%s' % set)

You can clarify your post if I'm misunderstanding.

Mike Shultz
  • 1,368
  • 1
  • 15
  • 32
  • 1
    Note that `set` is a keyword in python. So you might have to consider changing that name to something else more suited for this – smac89 Sep 16 '13 at 22:34