help with python difference between number
MRAB
python at mrabarnett.plus.com
Tue Jan 15 13:02:21 EST 2019
On 2019-01-15 14:07, achyuta2017 at gmail.com wrote:
>
> 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
> For e.g
>
> 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 as
> 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?
>
You didn't say Which version of Python you're using.
The "print>>" tells me that it's Python 2.
It's complaining about the '{}' in the format string.
Format strings were introduced in Python 2.6 and auto-numbering ('{}'
allowed instead of '{0}') was introduced in Python 2.7.
As it's complaining about a missing field name in '{}', it must be
Python 2.6, which is ancient!
And Python 2.7 reaches its end of life soon.
You should switch to Python 3 unless you have a very good reason for
staying on Python 2, and, if you must use Python 2, use Python 2.7.
More information about the Python-list
mailing list