473,386 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading from input file.

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
9 2162
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
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
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
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
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
"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
18
by: Michael | last post by:
Hi, I moved to c++ from c, and wanted to know what the best way to read data from files is in c++. Any thoughts? fscanf() is possible but fairly painful! Regards Michael
3
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
4
by: tushar.saxena | last post by:
Hi, I'm trying to read a file using the istearm class (I cant use ifstream since the input might be a file or it might be stdin). istream *input; // Add checks for file name here, else use...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.