Python Forum
python how to find difference between two values
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python how to find difference between two values
#1
Need help in giving me an idea on how to get the output

I am new to python.
My input is like this:
Output:
M <01/14/2019 08:07:01> Count:0 Total:50 Free: 20 A B M <01/14/2019 08:07:04> Count:1 Total:5 Free:10 A B M <01/14/2019 08:07:07> Count:2 Total:5 Free:3 A B
I am trying to make a output like where it prints the free and then the difference between the current free and previous free
Output:
M <01/14/2019 08:07:01> Count:0 Free: 20 M <01/14/2019 08:07:04> Count:1 Free: 10 absolute difference between time and prev time is -10 M <01/14/2019 08:07:07> Count:2 Free: 3 absolute difference between time and prev time is -7 .
And then later on i need to determine the time when we had the most negative free value.


I tried a code like this
Which printed
							 with open("summ4.txt") as f:
							# get first line/number
							nxt = int(next(f))
							for n in f:
								print("absolute difference between {} and {} = {}"
									  .format(n.rstrip(), nxt, abs(int(nxt) - int(n))))
								# set nxt equal to the next number
								nxt = int(next(f,0))
							   a=open('summ1.txt','r').readlines()
							   b=open('summ3.txt','r').readlines()
								with open('summ.txt','w') as out:
								  for i in range(0,365): 
						 print>>out,a[i].rstrip(),b[i]
.
i hit
Error:
Traceback (most recent call last): File "3.py", line 39, in <module> .format(n.rstrip(), nxt, abs(int(nxt) - int(n)))) ValueError: zero length field name in format
.
I guess my input file has a tab in the start and not able to get a difference rightly.
.
Any pointers on how to achieve the desired result?

Thanks,
Hare
Reply
#2
It seems that you are using an old version of python, isn't that the problem?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to find difference between fake driving license or real using python? pjaymn 5 2,869 Jun-14-2024, 07:01 AM
Last Post: Pedroski55
  find the sum of a series of values that equal a number ancorte 1 603 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  Any tips to how to find out values? nevlindan 1 792 Apr-27-2023, 09:14 PM
Last Post: deanhystad
  Loop through values in dictrionary and find the same as in previous row Paqqno 5 2,032 Mar-27-2022, 07:58 PM
Last Post: deanhystad
Question Find all values in drop down menu with bs4 DonaldBug13 1 4,258 Aug-06-2021, 06:16 PM
Last Post: Axel_Erfurt
  Find only the rows containing null values Bhavika 2 2,560 Jun-10-2020, 01:25 PM
Last Post: Bhavika
  python 3 find difference between 2 files pd007 2 2,296 May-22-2020, 01:16 AM
Last Post: Larz60+
  Find field name from all possible values kashcode 1 2,187 Apr-22-2020, 06:14 PM
Last Post: deanhystad
  How to find sum difference between two csv Prince_Bhatia 3 2,561 Jul-30-2019, 05:35 AM
Last Post: ndc85430
  Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu sultan 2 2,645 May-22-2019, 07:20 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020