76.21
76.83
77.50
78.50
…
…
I need to find out the difference of each of them consecutively. Means i need 76.83-76.21 , 77.50-76.83 , 78.50-77.50 like that. I can not split these numbers individually.
Could anyone please help
In my code i am trying to read all the numbers one by one. But the code is not giving the correct result. I am trying to put them in list . But the list file appears to be [l1],[l2],[l3],[l4]. I am trying to get the list in the form [l1,l2,l3,l4].
import numpy as np
from collections import defaultdict
import matplotlib.pyplot as plt
import re
def main():
with open(r'C:\Users\sarad\ss4.dat.log','r') as line:
table = defaultdict(dict)
for line in line:
if line:
entry = line.strip()
if ':' in entry:
t = entry
else:
if line.startswith("Sensor"):
_, sample,data = entry.split()
table[t].update({sample:float(data)})
data1=data.split(' ')
print(data1)
for i in range (len(data1)
dT=data1[i+1]-data[i]
print dT
df=pd.DataFrame(table).T
df.plot()
plt.xticks(
ticks=range(len(df)),
labels=df.index,
rotation=45
)
plt.locator_params(axis='x',nbins=10)
plt.xlabel('Time')
plt.ylabel('Temperature')
plt.title('Temperature vs Time ')
plt.show()
main()
This is going to be a problem. After the first execution, your file handle is gone. no, I’m wrong, somehow this works.
From reading your code it’s clear that the problem has a lot more parts than what you described earlier. It’s difficult to help without understanding what’s actually going on.
It works because the for loop grabs the thing to iterate over and holds onto it, so it doesn’t have to reevaluate each iteration. However, this IS still a very confusing line of code, and I would strongly recommend renaming the file to, say, file
Assuming the core difficulty here is “how do I check each overlapping, adjacent pair of the values, each time through the loop?”: in recent versions this is directly provided as itertools.pairwise like Kevin said. For more details and other approaches, please see: