1,105,633 Community Members

Need help with getting the difference of rows

Member Avatar
new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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!

Member Avatar
ihatehippies
Junior Poster
199 posts since Oct 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 2 [?]
 
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
Member Avatar
new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
ihatehippies
Junior Poster
199 posts since Oct 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 2 [?]
 
0
 

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

open('test.txt', 'rb') as fileobj:
Member Avatar
Lucaci Andrew
Master Poster
746 posts since Jan 2012
Reputation Points: 128 [?]
Q&As Helped to Solve: 116 [?]
Skill Endorsements: 18 [?]
 
0
 

use this instead

path=raw_input("Path: ")+'.txt'
x=1000
with open(path, 'rb') as fileobj:
...
Member Avatar
new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Error is

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

Member Avatar
ihatehippies
Junior Poster
199 posts since Oct 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 2 [?]
 
0
 

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

Member Avatar
ihatehippies
Junior Poster
199 posts since Oct 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 2 [?]
 
0
 
# 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]
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: