我有一个.msh文件,可以由文本阅读器打开和修改。我想用python打开它,然后用numpy数组替换一些特定的行,并将其另存为一个新的.msh文件。我的numpy数组有9列和数百行。我的文件也有数百行。我想用numpy数组替换文件中有9列的行。当然,numpy数组中的行数与文件中有9列的行数相同。为了简单起见,我在这里只显示了两行,但实际上我有数百行。 numpy阵列是:
arr_1= np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9],
[-1, 0, 0, 1, 46, 2, -11, 0, 0]])
我的文件(my_文件)如下所示:
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
2929
1 26.66002035140991 0.75 1.25
-1 5 14 13.2 7.4444 11 9 -3 0.15
0.2 9 54.45 1 63 22.45 0 12 425.65
然后,我想获得一个新保存的文件,其新名称如下:
$MeshFormat
2.2 0 8
$EndMeshFormat
$Nodes
2929
1 26.66002035140991 0.75 1.25
1 2 3 4 5 6 7 8 9
-1 0 0 1 46 2 -11 0 0
我只能做到以下几点,但它不起作用:
with open('my_file') as f:
new_data= line.split() for line in f if len(line.split()) == 9
for i in new_data:
for j in arr_1:
i = j
我试过了,但一点也不成功。所以,我很感激任何暗示和帮助。 干杯 阿里
请尝试以下操作:
您可以尝试此代码,其中将写入名为
changed
的新文件。为了减少磁盘写入(以提高性能,特别是对于大型文件),将向新文件写入大量行相关问题 更多 >
编程相关推荐