×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

logging to csv

logging to csv

logging to csv

(OP)
Hi,

I'm intend to do the following
-do a calculation ; record the value with timestamp to excel (csv format will do)
-repeat above step; append a new row to csv with newly calculated value and timestamp.
...
...
continue in endless loop.

Could someone give me some pointers on this.

Quote:

fd = open('my.csv','a')
fd.write(myCsvRow)
fd.close()

This should do the append bit, but stumped as to how to code the entire thing....

Thanks
David

RE: logging to csv

1st you may want to look at the csv module not that it is realy necessary.

the code snipet you have psoted should really be enough to get you satarted, perhaps you may want to check out a few turtorials, Dive into Python is one I found quite good

however to expand your code a bit:

CODE

import time
fd=open('my.csv','a')
while True:
    a=str(mycalc())  # your caululation function
    b=str(time.time())# time stamp
    fd.write("%s,%s\n" %(a,b)) #format for csv
 

I do not Have A.D.D. im just easily, Hey look a Squirrel!

RE: logging to csv

(OP)
import time,subprocess,csv
fd=open('my2.csv','a')
#spamWriter = csv.writer(open('eggs2.csv', 'wb'), delimiter=' ',quoting=csv.QUOTE_MINIMAL)
#while True:
proc = subprocess.Popen([" cat /home/myfile.txt |grep  -m 1 \"my game\""], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
#a=str(mycalc())  # your caululation function
b=str(time.time())# time stamp
fd.write("%s,%s\n" %(out,b)) #format for csv
#fd.write("%s\n" %(out)) #format for csv
#spamWriter.writerow([out])

Result:
 cat my2.csv
my game is football
,1300788809.37

Now trying with the csv module
import time,subprocess,csv
#fd=open('my2.csv','a')
spamWriter = csv.writer(open('eggs2.csv', 'wb'), delimiter=' ',quoting=csv.QUOTE_MINIMAL)
#while True:
proc = subprocess.Popen([" cat /home/myfile.txt |grep  -m 1 \"my game\""], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
#a=str(mycalc())  # your caululation function
b=str(time.time())# time stamp
#fd.write("%s,%s\n" %(out,b)) #format for csv
#fd.write("%s\n" %(out)) #format for csv
spamWriter.writerow([out,b])

Result:
cat eggs2.csv
"my game is football
" 1300789131.9

Still not getting formatted acc to what i'm looking for
expected is
my game is football 1300789131.9


 

RE: logging to csv

I suspect that the string returned as out contains a trailing return character
if you strip this you should get what you require.
examples:

CODE

>>>a="this is a test\n"
>>>print a
this is a test
>>>print repr(a)
"this is a test\n"
>>>print repr(a.strip())
"this is a test"

I do not Have A.D.D. im just easily, Hey look a Squirrel!

RE: logging to csv

(OP)
yes, i got around this with output = stdout.read().rstrip('\r\n')

now I get a file like

task1   1200
task2   1300
task3   1500

Now I require to label column 1 as tasks and column 2 as time
and then introduce a third column called 'duration'
to look

task1   1200
task2   1300   100
task3   1500   200

Is this possible or do i have to do it in excel?

 

RE: logging to csv

you need to store the value & subtract the new one each time through the loop
modify the wirte commend to write the information you require

if you do nut fully understand the current code then you should walk through some more python tutorials.

if i was to provide any further working code it would not help your learning process.
 

I do not Have A.D.D. im just easily, Hey look a Squirrel!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close