[visionegg] Re: how to output the result to a file

If you really like the print syntax and all the formatting, you can use the print statement to write to a file:

myf=open("mylogfile.txt",'w')
print >> myf, "This is the first line"
adict = {'key1':0, 'key2':'logfiletest'}
for i in range(10) :
        print >> myf, i, "of 10", adict

myf.close()

----
which gives the output:
$ cat mylogfile.txt
This is the first line
0 of 10 {'key2': 'logfiletest', 'key1': 0}
1 of 10 {'key2': 'logfiletest', 'key1': 0}
2 of 10 {'key2': 'logfiletest', 'key1': 0}
3 of 10 {'key2': 'logfiletest', 'key1': 0}
4 of 10 {'key2': 'logfiletest', 'key1': 0}
5 of 10 {'key2': 'logfiletest', 'key1': 0}
6 of 10 {'key2': 'logfiletest', 'key1': 0}
7 of 10 {'key2': 'logfiletest', 'key1': 0}
8 of 10 {'key2': 'logfiletest', 'key1': 0}
9 of 10 {'key2': 'logfiletest', 'key1': 0}


http://www.python.org/dev/peps/pep-0214/
http://docs.python.org/ref/print.html

Mark

On Nov 21, 2007, at 8:33 PM, Neil Halelamien wrote:

Speaking of data output, I've attached some code I wrote in the past
for logging data in a human-readable & machine-readable format, where
you can supply the names of variables in the current space that you'd
like to write to file. It uses what I think is a pretty nasty hack
that probably isn't guaranteed to work in future versions of Python,
but for the time being it's been useful to me.

On Nov 21, 2007 5:20 PM, Eamon Caddigan <ecaddiga@xxxxxxxx> wrote:
Here's a good tutorial on writing things to files (part of a larger
good tutorial on python): http://www.diveintopython.org/ file_handling/
file_objects.html#d0e15055

Note that writing to (and reading from) disk is a relatively slow
operation for a computer. It does its best by buffering output, but
if your experiment is sensitive to timing issues, it's typically best
to save data in another structure (e.g., a list of coordinate
tuples), and only write that info to a file at the end of your trials.

HTH,
Eamon



On Nov 21, 2007, at 7:01 PM, Lili Wu wrote:

Hi,
I have a question about how to output the running result of a program.
Take the demo of mouseTarget.py for example, I can use "print x,y "
to output every mouseposition in the Python Shell window. Is there
any method to output this data to a file, like a *.txt file?

Thanks!

Lili

雅虎邮箱,终生伙伴!

The Vision Egg mailing list
Archives: http://www.freelists.org/archives/visionegg
Website: http://www.visionegg.org/mailinglist.html


<DataLogging.py>

=====================================The Vision Egg mailing list
Archives: http://www.freelists.org/archives/visionegg
Website: http://www.visionegg.org/mailinglist.html

Other related posts: