954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with getting the difference of rows

Hi,
Just started learning python..and want to learn to do this

I want to read a text file that something like

10000 20000 30000 40000
20000 30000 88888 88888
60000 12333 44431 12345

I want to get the difference of numbers in column1 divided by x, where I want to give "x" as an argument, I am using x=1000 here

20000-10000=10000/1000=10
60000-20000=40000/1000=40

I want to print
0
10
40
and so on.

How can I do this?
Thanks for the help!

new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

it would probably look something like this

# untested
x = 1000
with open(path, 'rb') as fileobj:
   data = [x.strip().split(' ') for x in fileobj.readlines()]
total_rows = len(data)
for n in xrange(1, total_rows):
   first = int(data[n-1][0].strip())
   second = int(data[n][0].strip())
   print (second-first)/x
ihatehippies
Junior Poster
190 posts since Oct 2008
Reputation Points: 33
Solved Threads: 13
 

./rdiff.py
Traceback (most recent call last):
File "./rdiff.py", line 4, in
with open(test.txt, 'rb') as fileobj:
NameError: name 'test' is not defined

new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

you have to put test.txt in quotes otherwise python thinks you are referring to a variable.

open('test.txt', 'rb') as fileobj:
ihatehippies
Junior Poster
190 posts since Oct 2008
Reputation Points: 33
Solved Threads: 13
 

use this instead

path=raw_input("Path: ")+'.txt'
x=1000
with open(path, 'rb') as fileobj:
...
lucaciandrew
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 10
Solved Threads: 5
 

I am getting the correct values but, i would like to add a 0 as the first row and also fix the error below
2
0
13
1
Traceback (most recent call last):
File

new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Error is

Traceback (most recent call last):
File "./new.py", line 11, in
second = int(data[n][0].strip())
ValueError: invalid literal for int() with base 10: ''

new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

add 'print 0'
what are the items in the 5th and 6th column

ihatehippies
Junior Poster
190 posts since Oct 2008
Reputation Points: 33
Solved Threads: 13
 
# untested
x = 1000
with open(path, 'rb') as fileobj:
   data = [[y for y in x.strip().split(' ') if y] for x in fileobj.readlines()]
total_rows = len(data)
for n in xrange(1, total_rows):
   try:
      first = int(data[n-1][0].strip())
      second = int(data[n][0].strip())
      print (second-first)/x
   except ValueError:
      print "couldn't convert a number to integer, row %s"%n
      print data[n-1][0], data[n][0]
ihatehippies
Junior Poster
190 posts since Oct 2008
Reputation Points: 33
Solved Threads: 13
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: