473,804 Members | 3,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2193
Hi Kyrstian,

Try reading the file with ope(file,'r').r ead()
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.c om 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('inpu tfile1'), file('inputfile 2')):

# 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(fi le,'\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(fi le,'\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().spl it('\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.or g> 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.or g> 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
7059
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 file... Thank you to all of you who can help me with this one...
18
2246
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
2211
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
5231
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 writing into a dynamic array. My problem is that the array shows extra z and probably because of this further processing gives run time error in borland compiler. Can you please tell me, if the problem is related to handling end-of line , how do i do...
40
4610
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 line at a time and print it out. How can I do this? I wrote the code below but it's not correct since the fscanf reads one word (terminating in whitespace or newline) at a time, instead of reading the whole line. #include <stdio.h> void...
0
2198
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 CSV file without any issues only if all the fields are not null. If any field is blank, it moves the values to the left and displays the value under invalid column. Example is shown below: A part of CSV file. I am reading the first row as...
21
3072
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 00000000_00 0 000 000 000 0000 000 I need to read the file line by line and eventually parse out each piece of the file and store in arrays that correspond to the specific
4
1939
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 input = &cin; while (!input->eof())
6
3535
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.