我在一个班级里有这个功能
def readInput(self, inFile):
with open(inFile, "r") as file:
for line in file:
if (line.split()[0] == "Ks"):
nextLine = next(file)
self.Ks = nextLine.split()[0]
self.thetaS = nextLine.split()[1]
self.thetaR = nextLine.split()[2]
self.alpha = nextLine.split()[3]
self.lamb = nextLine.split()[4]
self.n = nextLine.split()[5]
Run Code Online (Sandbox Code Playgroud)
它基本上是"Ks"在输入文件(inFile)中搜索模式(),以存储inFile内部实例变量的下一行的变量值。
代码中有很多重复,我认为有可能以一种聪明(而且更短)的方式编写代码。
有任何想法吗?
输入文件如下所示:
### Soil hydraulic properties. Units: m, d
Ks ThetaS ThetaR alpha lambda n
0.128 0.01 0.42 0.84 -1.497 1.441
Run Code Online (Sandbox Code Playgroud)
使用元组拆包:
self.Ks, self.thetaS, self.thetaR, self.alpha, self.lamb, self.n = nextLine.split()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39 次 |
| 最近记录: |