How to read data from files and process it?

In summary, the speaker is having trouble with their code giving them an incorrect output of 0.00000. They are attempting to read data from a file with four columns and process it for further calculation. The structure of the code includes several OPEN statements and a DO loop. The speaker suggests adding the option FORM=FORMATTED if the data is in human readable form or FORM=UNFORMATTED and RECL=xxx if the data is binary.
  • #1
ade_tokunbo
2
0
Dear all,

I want to read data from a file which has four column and process the data for further calculation. After running the code it keeps given me 0.00000 as output. What do i do? Here is the structure of the code

program new

Implicit none
integer,parameter ::Nmax=1000
Integer :: i,io,max,t
Real*8,Dimension(Nmax) :: Ev,X,Y,Z
Real*8, Dimension(Nmax)::Evr,Xr,Yr,Zr


open(1, File='si.dat',status='old')

open(16,file='cal.dat',status='unknown')

max=0
do i=1,Nmax
read(1,*,IOSTAT = io)Ev(i),X(i),Y(i),Z(i)
if (io.eq.0) then
max = max+1
else
goto 677
endif
enddo


677 close(1)

do t=1,Nmax
open(1, file='si.dat',status='old')
do i=1,max

read(1,*,iostat=io)Ev(i),X(i),Y(i),Z(i)
enddo
write(6,*)Ev(t),X(t),Y(t),Z(t)
enddo
close(16)

end program
 
Technology news on Phys.org
  • #2
On the OPEN statements, if the data is in human readable form (i.e. text), add the additional option FORM=FORMATTED. If the data is binary, then FORM=UNFORMATTED and RECL=xxx should be added, but in any event, you'll need to know what form the data files are in.
 

1. How do I open a file in Python?

To open a file in Python, you can use the built-in open() function. This function takes two parameters: the name of the file you want to open and the mode in which you want to open it (e.g. "r" for read mode, "w" for write mode). Once the file is opened, you can use other methods like read() or readlines() to access the data inside the file.

2. How do I read data from a file in Python?

After opening a file, you can use methods like read() or readlines() to read the data inside the file. read() will return the entire contents of the file as a string, while readlines() will return a list of strings, each representing a line in the file. You can then use string manipulation or data processing techniques to further process the data.

3. How can I handle errors while reading files in Python?

To handle errors while reading files, you can use the try-except block. This allows you to catch any errors that may occur while reading the file and handle them accordingly. For example, you can print an error message or perform a different action if the file cannot be opened or read.

4. What is the difference between reading a file in "text mode" vs "binary mode"?

When opening a file, you can specify the mode as "t" for text mode or "b" for binary mode. Text mode is used for files that contain human-readable text, while binary mode is used for files that contain non-textual data, such as images or audio files. Depending on the type of data in your file, you will need to choose the appropriate mode for reading and processing it.

5. Can I read data from multiple files at once in Python?

Yes, you can read data from multiple files at once in Python by using a for loop to iterate through a list of file names. Inside the loop, you can use the open() function to open and read each file, and then process the data as needed. This is a useful technique for handling large datasets or when you need to combine data from multiple files.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
745
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
Back
Top