mccoy
Posts: 15
Joined: Tue Mar 07, 2017 11:41 pm

Subtracting values from the same list

Tue Apr 11, 2017 5:44 pm

Hi, I have what I feel like is a pretty easy problem but my I'm not knowledgeable enough about Python to figure it out.
I was given a script that takes time.time before and after a GPIO output command to test the jitter of the commands. It stored the data in a text file like:
  • tp0
    1491592477.284164
    1491592477.284201
    1491592477.284322
    1491592477.294415
    tp1
    1491592482.30128
    1491592482.301307
    1491592482.301389
    1491592482.311484
This repeats to 300 trials. I need to take to subtract the second time by the first and the fourth from the third. So
  • tp0
    1491592477.284164 (Subtract this)
    1491592477.284201 (From this)
    1491592477.284322 (and this one)
    1491592477.294415 (from this one)

Code: Select all

infile = "/Users/Caghain/Desktop/LAB CODE/TIME/GPIO_times_Edit.txt"
editfile = "/Users/Caghain/Desktop/LAB CODE/TIME/GPIO_times_Subtract.txt"

fin = open(infile, "w+")
fedit = open(editfile, "w+")

for x in fin:
    line_TTL=[x+2]
    line_SNDOUT=[x+3]
    GPIO_HIGH = line_SNDOUT - line_TTL
    fedit.write(GPIO_HIGH)

fedit.close() 
The above doesn't work, but that was my attempt. Thanks for any help.

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Subtracting values from the same list

Tue Apr 11, 2017 6:02 pm

Hello, in pseudocode I would write:

Code: Select all

open fin
open fout
try
    while True:
        a = fin readline
        b = fin readline
        fout write ( float(a) - float(b) )
except:
    pass
finally
    close fin
    close fout
Hope this helps,
Gerhard

pcmanbob
Posts: 9465
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Subtracting values from the same list

Tue Apr 11, 2017 10:15 pm

Hi.

so not having your original file to test against created a test file from your posted results

Code: Select all

tp0
477.284164
477.284201
477.284322
477.294415
tp1
1491592482.30128
1491592482.301307
1491592482.301389
1491592482.311484
tp2
477.284164
477.284201
477.284322
477.294415
tp3
1491592482.30128
1491592482.301307
1491592482.301389
1491592482.311484
tp4
477.284164
477.284201
477.284322
477.294415
I hope this is the correct layout.

so this is the code I came up with

Code: Select all

#!/usr/bin/python
infile = "/home/pi/edit.txt"
outfile = "/home/pi/answer.txt"
count = sum(1 for line in open(infile,"r"))

result = open(infile,"r")
answer = open(outfile,"a")
while count > 0:
	
	valuea = (result.readline())
	if len(valuea) <= 5:
		print valuea
		answer.write (str(valuea))
		count = count - 1
	else:
		valuea = float(valuea)
		print "a", valuea
		valueb = float(result.readline())
		print "b", valueb
		valuec = valueb - valuea
		print "result", valuec
		answer.write (str(valuec) + "\n")
		count = count - 2
		
result.close
answer.close
and these were the results in the answer file

Code: Select all

tp0
3.70000000203e-05
0.010093
tp1
2.69412994385e-05
0.0100951194763
tp2
3.70000000203e-05
0.010093
tp3
2.69412994385e-05
0.0100951194763
tp4
3.70000000203e-05
0.010093
now you will have to change the infile and outfile references to match you original posted values
so hope it works and does as you wish, you can remove all the print statements if you wish, please check I have the math round the right way.
if it does not work as expected please let me know with any error messages.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

mccoy
Posts: 15
Joined: Tue Mar 07, 2017 11:41 pm

Re: Subtracting values from the same list

Thu Apr 13, 2017 2:27 pm

ghp wrote:Hello, in pseudocode I would write:

Code: Select all

open fin
open fout
try
    while True:
        a = fin readline
        b = fin readline
        fout write ( float(a) - float(b) )
except:
    pass
finally
    close fin
    close fout
Hope this helps,
Gerhard
Thanks!

mccoy
Posts: 15
Joined: Tue Mar 07, 2017 11:41 pm

Re: Subtracting values from the same list

Thu Apr 13, 2017 2:27 pm

pcmanbob wrote:Hi.

so not having your original file to test against created a test file from your posted results

Code: Select all

tp0
477.284164
477.284201
477.284322
477.294415
tp1
1491592482.30128
1491592482.301307
1491592482.301389
1491592482.311484
tp2
477.284164
477.284201
477.284322
477.294415
tp3
1491592482.30128
1491592482.301307
1491592482.301389
1491592482.311484
tp4
477.284164
477.284201
477.284322
477.294415
I hope this is the correct layout.

so this is the code I came up with

Code: Select all

#!/usr/bin/python
infile = "/home/pi/edit.txt"
outfile = "/home/pi/answer.txt"
count = sum(1 for line in open(infile,"r"))

result = open(infile,"r")
answer = open(outfile,"a")
while count > 0:
	
	valuea = (result.readline())
	if len(valuea) <= 5:
		print valuea
		answer.write (str(valuea))
		count = count - 1
	else:
		valuea = float(valuea)
		print "a", valuea
		valueb = float(result.readline())
		print "b", valueb
		valuec = valueb - valuea
		print "result", valuec
		answer.write (str(valuec) + "\n")
		count = count - 2
		
result.close
answer.close
and these were the results in the answer file

Code: Select all

tp0
3.70000000203e-05
0.010093
tp1
2.69412994385e-05
0.0100951194763
tp2
3.70000000203e-05
0.010093
tp3
2.69412994385e-05
0.0100951194763
tp4
3.70000000203e-05
0.010093
now you will have to change the infile and outfile references to match you original posted values
so hope it works and does as you wish, you can remove all the print statements if you wish, please check I have the math round the right way.
if it does not work as expected please let me know with any error messages.
Perfect! Thanks, this got me definitely in the right direction.

Return to “Python”