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!

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

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

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

open('test.txt', 'rb') as fileobj:

use this instead

path=raw_input("Path: ")+'.txt'
x=1000
with open(path, 'rb') as fileobj:
...

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

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

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

# 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]
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.