hi all........

file1
99 root S 5176 95 41.2 8.3 snmpd

file2
99 root S 5176 95 1.0 8.3 snmpd


i want to caluculate difference between two files fifth column(i.e bolded columns as mentioned above)..


thanks in advance...:'(:)

Are all the values seperated by spaces?

If so then look into the .split() function. It splits a string by the inputed paramter.

E.G. to split a string by spaces you would put

x = stringname.split(" ")
This will make an array with
x[0] = 99
x[1] = root
...

Doing x[4] will give you the fifth column. (because arrays start at value 0)

If its opening files you're having problems with. All you have to do is

file1 = open("filepath/name", "r")

r = read, w = write

then do

text = file1.read()

to read the whole file, or

line = file1.readline()

if you wish to read a single line at a time.

Here you go my friend :)

file1="".join('99 root S 5176 95 41.2 8.3 snmpd'.split()[5:6])
file2= "".join('99 root S 5176 95 1.0 8.3 snmpd'.split()[5:6])

print(float(file1)- float(file2))
## output
40.2

Hope it helps.

Wrong post.

Another way to do this.

"""
file_1.txt-->
99 root S 5176 95 41.2 8.3 snmpd

file_2.txt-->
99 root S 5176 95 1.0 8.3 snmpd
"""

with open('file_1.txt') as file_1, open('file_2.txt') as file_2:
     f1 = [float(item.split()[5]) for item in file_1]
     f2 = [float(item.split()[5]) for item in file_2]
     print f1[0] - f2[0] #40.2

when i run this ,it is showing error

with open(('file2.txt') as file_1):

SyntaxError: invalid syntax

with open(('file2.txt') as file_1):

That is not my code,you can just copy it.
Now you have made 3 error in that short line.
Just compare then you should see it.

With older versions of Python you might have to use ...

"""
file_1.txt-->
99 root S 5176 95 41.2 8.3 snmpd

file_2.txt-->
99 root S 5176 95 1.0 8.3 snmpd
"""

from __future__ import with_statement

with open('file_1.txt') as file_1:
    with open('file_2.txt') as file_2:
        f1 = [float(item.split()[5]) for item in file_1]
        f2 = [float(item.split()[5]) for item in file_2]
        print f1[0] - f2[0]  # 40.2

hi.....
wen i run this i m getting error ....invalid syntax


from __future__ import with_statement
with open('file_1.txt') as file_1:
with open('file_2.txt') as file_2:
f1 = [float(item.split()[5]) for item in file_1]
f2 = [float(item.split()[5]) for item in file_2]
print f1[0] - f2[0] # 40.2

Use code tag,are you using python 3?
Then you have to change print statement,because as mention many times print is a function in python 3.

#python 3
"""
file_1.txt-->
99 root S 5176 95 41.2 8.3 snmpd

file_2.txt-->
99 root S 5176 95 1.0 8.3 snmpd
"""

with open('file_1.txt') as file_1:
    with open('file_2.txt') as file_2:
        f1 = [float(item.split()[5]) for item in file_1]
        f2 = [float(item.split()[5]) for item in file_2]
        print (f1[0] - f2[0])  # 40.2

i m using python 2.5 ,i m getting error in the first line(i.e,with open line)

i m using python 2.5 ,i m getting error in the first line(i.e,with open line)

Why are using an so old version as 2.5?.
I shall test later on my laptop there i have 2.5 and see if it work.
The with statement did come with the release of python 2.5.
http://docs.python.org/whatsnew/2.5.html#pep-343-the-with-statement
You can try to add this as first line.
from __future__ import with_statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.