我有一个文本文件,其中前几行是文本,其余行包含实数形式的数据。我只需要将实数数组存储在新文件中。为此,我读取输出正确的文件的总行数,然后尝试从特定行号中读取实数。我无法理解如何读取这些数据。以下是文件的一部分。我也有很多这样的文件要读。
AptitudeQT paperI: 12233105
Latitude : 30.00 S
Longitude: 46.45 E
Attemptone Time: 2017-03-30-09-03
End Time: 2017-03-30-14-55
Height(agl): m
Pressure: hPa
Temperature: deg C
Humidity: %RH
Uvelocity: cm/s
Vvelocity: cm/s
WindSpeed: cm/s
WindDirection: deg
---------------------------------------
10 1008.383 27.655 62.200 -718.801 -45.665 720.250 266.500
20 1007.175 27.407 62.950 -792.284 -18.481 792.500 268.800
有很多例子可以跳过/阅读这样的行
但总而言之,选项 A 是跳过标头并只读数据:
! Skip first 17 lines
do i = 1, 17
read (unit,*,IOSTAT=stat) ! Dummy read
if ( stat /= 0 ) stop "error"
end do
! Read data
do i = 1, 1000
read (unit,*,IOSTAT=stat) data(:,i)
if ( stat /= 0 ) stop "error"
end do
如果你有很多这样的文件,我建议把它包装在一个子例程/函数中。
选项 B 是使用 unixtail
实用程序丢弃标头(更多信息在这里(:
tail -n +18 file.txt