By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,785 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Reading from input file.

P: n/a
Dear Python Community,

I am an engineering and I am experiencing some trouble. Having output
data from other software I want to use it. To achieve this I decided to
use Python since this language is the best known for me.

So.. for my future Python script, the input data are in form of:
1
1233.2E-3 2123.2323 2E+2 3453.3E+1
1233.2E-3 2123.2323 2E+2 3453.3E+1
1233.2E-3 2123.2323 2E+2 3453.3E+1
2
1233.2E-3 2123.2323 2E+2 3453.3E+1
1233.2E-3 2123.2323 2E+2 3453.3E+1
1233.2E-3 2123.2323 2E+2 3453.3E+1
and so on...

The number 1 and 2 and so on, are the numbers of measurments, which are
different in number depending on analyzed case. So sometimes it could
be 3 and sometimes 1000 or more.

What I want to achieve, I want to compare or rather get difference
between two similar input files. I tried to work with "readline"
command, however it is hard to use it (for me) since input files are
different in its size (3,1000 or more input points). Therefore, I
decided to ask you for help how to do it. Esspecially how to make that
python script which will be reading data, each line, and each column
of input file and therefore will compare it with different input file
giving finally difference size between each measures for example:
First data at point 1 minus first data from point 1 from different
input file:
(1233.2E-3) - (1233.3E-3) = -0.1E-3

Looking forward for you answer,

Best regards,

Krystian

Jan 11 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi Kyrstian,

Try reading the file with ope(file,'r').read()
and then split the string using '\n' as the delimiter (this will break
the file down into each individual line consisting of several columns).
Then string split each line of the file (unsing ' ' as the delimiter),
they should now be 3, or more, columns per line, and store each column
in a vector - adding each line to the vector with append function. Then
place each vector in a list.
Now you should have a file to compare with the next one. But watch out
for files of varying lengths as they cannot be compared on a one-to-one
basis. One you have each file in a list or vector or dictionary or
array style, comparison and calculations should be straight forward.

Hope this points you in the right direction. I haven't tried this
myself but I have done a similar thing earlier but I strongly believe
that it should work.

Sincerely,
Sheldon

Jan 11 '06 #2

P: n/a
I do not clearly understand what you say.

I am no going to change input files in anyway. I just want to read them
with Python, and postprocess.

I understand:
open(inputfile.txt, 'r').read()
However what you mean saying split the string using '\n'. Where should
I put it? After file name, or what? Sorry, but I am still learing
Python.

Looking forward hearing from you,

Regards

Jan 11 '06 #3

P: n/a
I do not clearly understand what you say.

I am no going to change input files in anyway. I just want to read them
with Python, and postprocess.

I understand:
open(inputfile.txt, 'r').read()
However what you mean saying split the string using '\n'. Where should
I put it? After file name, or what? Sorry, but I am still learing
Python.

Looking forward hearing from you,

Regards

Jan 11 '06 #4

P: n/a
hi!

i find it rather hard to understand your problem, but i'll try anyway:

pa*****@gmail.com schrieb:
So.. for my future Python script, the input data are in form of:
1
1233.2E-3 2123.2323 2E+2 3453.3E+1
1233.2E-3 2123.2323 2E+2 3453.3E+1
1233.2E-3 2123.2323 2E+2 3453.3E+1
2
1233.2E-3 2123.2323 2E+2 3453.3E+1
1233.2E-3 2123.2323 2E+2 3453.3E+1
1233.2E-3 2123.2323 2E+2 3453.3E+1
and so on...
are these 1's and 2's there to indicate different files, or do
they really appear in the input files?
The number 1 and 2 and so on, are the numbers of measurments, which are
different in number depending on analyzed case. So sometimes it could
be 3 and sometimes 1000 or more.

What I want to achieve, I want to compare or rather get difference
between two similar input files. I tried to work with "readline"
command, however it is hard to use it (for me) since input files are
different in its size (3,1000 or more input points). Therefore, I
decided to ask you for help how to do it. Esspecially how to make that
python script which will be reading data, each line, and each column
of input file and therefore will compare it with different input file
giving finally difference size between each measures for example:
First data at point 1 minus first data from point 1 from different
input file:
(1233.2E-3) - (1233.3E-3) = -0.1E-3


well, from what i understood you might try something like this:

-------------------------------
from itertools import izip

# iterate over two files at once (stopping at the shorter ones end)
for lines in izip(file('inputfile1'), file('inputfile2')):

# split the lines into columns
columns = [line.split() for line in lines]

# calculate and print the difference of the individual entries
for x, y in izip(*columns):
print float(x)-float(y),
print
-------------------------------

if this (or something similar) does not work for you, you could take
a look at the difflib module:

http://docs.python.org/lib/module-difflib.html

hope that helps, David.
Jan 11 '06 #5

P: n/a
Hi,

after you have read the file then split it like this:
file = open('inputfile.txt', 'r').read()
import string
file = string.split(file,'\n')
now if you print file[0] you should only get the first line.
Be careful and examine the file for non-continuous sections where a
line is empty. That is to say where file[x] = ' '. You should remove
these lines from the txt files before you read it with python. It maybe
so that python reads the entire file, empty spaces and all, and then
you have to remove them after the split with a WHERE statement. This is
a much easier way so lets hope that this is so.

I hope this was more clear and helpful this time around.

Cheers
Sheldon

Jan 12 '06 #6

P: n/a
"Sheldon" <sh******@gmail.com> writes:
after you have read the file then split it like this:
file = open('inputfile.txt', 'r').read()
import string
file = string.split(file,'\n')
You're doing things the hard way - at least if you have a modern
Python. The above can be done as:

file = open('inputfile.txt', 'r').read().split('\n')

Or even better

file = open('inputfile.txt', 'r').readlines()

will all give you the same result.

BTW, it's a bad habit to leave open files laying around. It's also a
bad habit to use the name of builtins (like "file") as variable names.
now if you print file[0] you should only get the first line.
Be careful and examine the file for non-continuous sections where a
line is empty. That is to say where file[x] = ' '. You should remove
these lines from the txt files before you read it with python. It maybe
so that python reads the entire file, empty spaces and all, and then
you have to remove them after the split with a WHERE statement. This is
a much easier way so lets hope that this is so.


Huh? What in the OP makes you think this is even necesary?

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jan 12 '06 #7

P: n/a
So Mike if you can do better then do it then! There are many ways do
solve a problem, perhaps you have not learned that yet. At first this
guy didn't know what to do, so he had to begin somewhere. Now you can
take him much further, I am sure but the journey might not be so
pleasant. Your attitude really needs major adjustment but then again, I
have to consider the source.
FYI, the file[0] part was simply to check if it worked.

Don't waste your time with me and my "hard way", it is Krystian you
should be helping!

Jan 12 '06 #8

P: n/a
"Sheldon" <sh******@gmail.com> writes:
So Mike if you can do better then do it then! There are many ways do
solve a problem, perhaps you have not learned that yet. At first this
guy didn't know what to do, so he had to begin somewhere. Now you can
take him much further, I am sure but the journey might not be so
pleasant. Your attitude really needs major adjustment but then again, I
have to consider the source.


Let's double check: I didn't insult you. I didn't insult the original
poster. I didn't say the code was bad, or ugly, or otherwise
intolerable. All I did was point out that - with a modern Python -
there's a shorter, easier to read way to write it. Pretty typical
c.l.python behavior: demonstrating how posted code can be improved in
some way. Personally, I encourage this behavior, because it helps
everybody write better code.

In return, I get insulted, told I need an attitude adjustment, and
insulted a second time.

If seeing people improve your code ticks you off so badly, you should
think *very* hard before you post it to c.l.python. Writing code that
can't be improved is nearly impossible, so it's very likely that any
code posted here - by anyone - someone will know how to improve.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jan 12 '06 #9

P: n/a
Sorry Mike, after seeing so many "experts" beat up others for not being
as "smart" as they are, I intrepreted your words incorrectly - my
apologies. I am not in the least bit against impproving my programming.
I liked what you did and thanks for the pointers.

Sheldon

Jan 13 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.