读写制表符分隔的文本数据 [英] Read and write tab-delimited text data

查看:281
本文介绍了读写制表符分隔的文本数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 温度H2O CO2 N2 NH3 
10 2.71539我有一个以制表符分隔格式的excel输出: E + 12 44374931376 7410673406 2570.560804
20 2.34216E + 12 38494172272 6429230649 3148.699673
30 2.04242E + 12 33759520581 5639029060 3856.866413
40 1.75491E + 12 29172949817 4882467457 4724.305292



我需要将这些数字转换为 FORMAT(1X,F7.0, 2X,1P4E11.3)可读取其他代码。
这就是我想到的:

pre $ program $

real temp ,neuts(4)
整数i,j
字符标题

open(11,file ='。/ unformatted.txt',status ='old')
打开(12,file ='。/ formatted.txt',status ='unknown')

读(11,*)头
写(12,*)头$ b $ (11,*)temp,(neuts(j),j = 1,4)
写(12,23)temp,(neuts( j),j = 1,4)
完成

23格式(1X,F7.0,2X,1P4E11.3)
关闭(11)
close(12)

return
end

这个错误:

pre $ Fortran运行时错误:列表输入项目1中的实数不正确

解决方案有没有其他的方法可以将数据转换成这种格式?

您需要一个字符串,而不是一个字符作为标题

 字符(80)header 

除此之外,您的程序适用于我。确保你的循环中有正确的行数

  Do i = 1,200 

200 调整为数据行的实际数量。 b

如果由于某些原因,您甚至无法读取单行,您也可以使用以下格式:

  read(11,'(f2.0,4(1x,f11.0))')temp,(neuts(j),j = 1,4)
pre>

因为该标签只是一个字符,您可以轻松跳过。



非格式化和格式化意味着在Fortran中完全不同的东西。无格式是你可能知道的二进制。

为程序使用一些缩进和空行来使它们可读。



没有理由明确地使用 status = unknown 。只是不要把东西放在那里。在你的情况下 status = replace 可能更合适。
$ b

FORMAT 语句已经过时了,在现代的Fortran中,我们使用格式化字符串:

$ $ $ $ $ $ $ $ $写入(12,'(1X,F7 .0,2X,1P4E11.3)')temp,(neuts(j),j = 1,4)

在结束之前绝对没有理由让你的 return 。退货是为了尽早从程序返回。有些人在结束程序之前放置了 stop ,但这是多余的。


I have an excel output in the tab-delimited format:

temperature H2O  CO2     N2      NH3     
10  2.71539E+12 44374931376 7410673406  2570.560804
20  2.34216E+12 38494172272 6429230649  3148.699673
30  2.04242E+12 33759520581 5639029060  3856.866413
40  1.75491E+12 29172949817 4882467457  4724.305292
.
.
.

I need to convert these numbers to FORMAT(1X,F7.0,2X,1P4E11.3) readable for another code. This is what I've come up with:

 program fixformat

  real temp, neuts(4)
  integer i,j
  character header

  open(11,file='./unformatted.txt',status='old')
  open(12,file='./formatted.txt',status='unknown')

  read(11,*) header
  write(12,*) header

  do i = 1, 200
    read(11,*) temp, (neuts(j),j=1,4)
    write(12,23) temp, (neuts(j),j=1,4)
  end do

23    FORMAT(1X,F7.0,2X,1P4E11.3)
  close(11)
  close(12)

  return
 end

I keep getting this error:

Fortran runtime error: Bad real number in item 1 of list input

Is there any other way to convert the data to that format?

解决方案

You need a character string, not a single character for the header

character(80)  header

other than that you program works for me. Make sure you have the right number of lines in your loop

Do i=1,200

Adjust 200 to the real number of your data lines.

If for some reason you still cannot read even a single line, you can also use the format:

read(11,'(f2.0,4(1x,f11.0))') temp, (neuts(j),j=1,4)

because the tab is just a character you can easily skip.

Notes:

Unformatted and formatted means something completely different in Fortran. Unformatted is what you may know as "binary".

Use some indentation and blank lines for your programs to make them readable.

There is no reason to explicitly use status=unknown. Just don't put anything there. In your case status=replace may be more appropriate.

The FORMAT statement is quite obsolete, in modern Fortran we use format strings:

write(12,'(1X,F7.0,2X,1P4E11.3)') temp, (neuts(j),j=1,4)

There is absolutely no reason for your return before the end. Returns is for early return from a procedure. Some put stop before the end program, but it is superfluous.

这篇关于读写制表符分隔的文本数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆