我有一个Fortran程序,它从打开和读取.txt文件中的数据开始.在程序结束时,将写入一个新文件,该文件将替换旧文件(最初导入的文件).
但是,可能会发生需要打开的文件不存在,对于这种情况,应该从.txt文件导入的变量应该是0.
我想通过下面的代码执行此操作,但是这不起作用,并且当文件history.txt不存在时脚本被中止.
当history.txt文件不存在时,如何让脚本将默认值设置为我的变量?
OPEN(UNIT=in_his,FILE="C:\temp\history.txt",ACTION="read")
if (stat .ne. 0) then !In case history.txt cannot be opened (iteration 1)
write(*,*) "history.txt cannot be opened"
KAPPAI=0
KAPPASH=0
go to 99
end if
read (in_his, *) a, b
KAPPAI=a
KAPPASH=b
write (*, *) "KAPPAI=", a, "KAPPASH=", b
99 close(in_his)
Run Code Online (Sandbox Code Playgroud)
导入的文件非常简单,如下所示:
9.900000000000006E-003 3.960000000000003E-003
Run Code Online (Sandbox Code Playgroud) 我想从文本文件导入几个坐标(最多可以添加20.000).需要将这些坐标添加到列表中,如下所示:
coords = [[0,0],[1,0],[2,0],[0,1],[1,1],[2,1],[0,2],[1,2],[2,2]]
Run Code Online (Sandbox Code Playgroud)
但是,当我想导入坐标时,我得到了以下错误:
invalid literal for int() with base 10
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何正确导入坐标.有没有人有任何建议为什么这不起作用?我认为创建整数存在一些问题.我使用以下脚本:
Bronbestand = open("D:\\Documents\\SkyDrive\\afstuderen\\99 EEM - Abaqus 6.11.2\\scripting\\testuitlezen4.txt", "r")
headerLine = Bronbestand.readline()
valueList = headerLine.split(",")
xValueIndex = valueList.index("x")
#xValueIndex = int(xValueIndex)
yValueIndex = valueList.index("y")
#yValueIndex = int(yValueIndex)
coordList = []
for line in Bronbestand.readlines():
segmentedLine = line.split(",")
coordList.extend([segmentedLine[xValueIndex], segmentedLine[yValueIndex]])
coordList = [x.strip(' ') for x in coordList]
coordList = [x.strip('\n') for x in coordList]
coordList2 = []
#CoordList3 = [map(int, x) for x in coordList]
for i in …Run Code Online (Sandbox Code Playgroud)