Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...The level of expertise is awesome. The nature in which people respond is professional helpful and not the least condescending. I can't say that for most forums..."

Geography

Where in the world do Tek-Tips members come from?
blacknred (TechnicalUser)
22 Mar 11 1:06
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
Helpful Member!  IPGuru (TechnicalUser)
22 Mar 11 4:58
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!

blacknred (TechnicalUser)
22 Mar 11 6:22
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


 
IPGuru (TechnicalUser)
22 Mar 11 8:55
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!

blacknred (TechnicalUser)
22 Mar 11 11:40
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?

 
IPGuru (TechnicalUser)
22 Mar 11 12:35
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!

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!

Back To Forum

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