sxq2221
sxq2221

Reputation: 57

Error in Reading a txt file: Bad integer for item in list input

I have this code to read numeric values that are saved by Fortran. That data has 4800 rows and 19 column.

The head of this data looks like this

       1           2   1.50000000       0.00000000               1          21         240         361           2           0           0           0           0           0           0           0           0           0           0
       2           2   1.50000000       0.00000000               1          19         208         390           7           0           0           0           0           0           0           0           0           0           0
       3           2   1.50000000       0.00000000               1          13         181         419          11           0           0           0           0           0           0           0           0           0           0
       4           2   1.50000000       0.00000000               1          17         213         386           8           0           0           0           0           0           0           0           0           0           0
       5           2   1.50000000       0.00000000               1          18         215         389           2           0           0           0           0           0           0           0           0           0           0
       6           2   1.50000000       0.00000000               1          18         245         356           5           0           0           0           0           0           0           0           0           0           0
       7           2   1.50000000       0.00000000               1          17         205         395           7           0           0           0           0           0           0           0           0           0           0
       8           2   1.50000000       0.00000000               1          21         251         350           2           0           0           0           0           0           0           0           0           0           0
       9           2   1.50000000       0.00000000               1          17         207         385          15           0           0           0           0           0           0           0           0           0           0
      10           2   1.50000000       0.00000000               1          10         142         439          33           0           0           0           0           0           0           0           0           0           0
      11           2   1.50000000       0.00000000               1          19         249         353           3           0           0           0           0           0           0           0           0           0           0

The name of the columns are, sim, infected, beta, epsilon. The other 15 columns "countt" is just the simulation results. I wrote this code to read the data in F90.

    program read
    implicit none 
     integer, dimension(4800,15)::countt
     integer :: i
    integer ,dimension(4800)::sim, infected,id
    real,dimension(4800):: epsilon, beta

 open(unit = 2, file = '/Dropbox/epi_data.txt', status = 'old', action = 'read')
  do i = 1,4800
 read(2,*) sim(i), infected(i), beta(i), epsilon(i),  countt(i,:)

  print*,  sim(i), infected(i), beta(i), epsilon(i),  countt(i,:)
end do
  close(2)

  end program read

Whenver I run this code, I get this error " Fortran runtime error: Bad integer for item 3 in list input "

What am I missing here?

The other issue I have is, what should I do if I want to save a training set by by 100. I mean, save 100 rows and skip a 100, save a 100 and skip a 100 until the end of rows? So at the end of my 4800rows, I will have 2400 for training set.

Do I need to upload the data for this question?

Upvotes: 0

Views: 1047

Answers (2)

Hải Phạm Thanh
Hải Phạm Thanh

Reputation: 23

you have to reformat your *.txt file.

Fortran uses dot "." as a decimal separation, such as 1.50000000 is "1.5", not "150000000".

Upvotes: 0

Alexander Vogt
Alexander Vogt

Reputation: 18118

You are trying to read in the third column as integer values (infected is of type integer). However, 1.50000000 and the like are not formatted as integers (they are reals). Therefore the error.

You probably need to change the order of the columns or how you read them in, i.e. remove id(i),.

Upvotes: 3

Related Questions