We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,201 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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!

3
Contributors
8
Replies
17 Hours
Discussion Span
1 Year Ago
Last Updated
9
Views
new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
197 posts since Oct 2008
Reputation Points: 33
Solved Threads: 16
Skill Endorsements: 1

./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

new2pyp
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
197 posts since Oct 2008
Reputation Points: 33
Solved Threads: 16
Skill Endorsements: 1

use this instead

path=raw_input("Path: ")+'.txt'
x=1000
with open(path, 'rb') as fileobj:
...
Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

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
Skill Endorsements: 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: ''

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

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

ihatehippies
Junior Poster
197 posts since Oct 2008
Reputation Points: 33
Solved Threads: 16
Skill Endorsements: 1
# 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
197 posts since Oct 2008
Reputation Points: 33
Solved Threads: 16
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1539 seconds using 2.7MB