Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

logging to csv 1

Status
Not open for further replies.

blacknred

Technical User
Nov 24, 2010
12
IN
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.

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
 
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!
 
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


 
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!
 
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?

 
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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top