使用python读取nastran几何文件 [英] Reading nastran geometry file with python
问题描述
我正在尝试使用python解析nastran文件的几何信息.我目前的尝试使用NumPy以及正则表达式.快速读取数据并确保结果为NumPy数组非常重要.
I am trying to parse the geometry information of a nastran file using python. My current attempts use NumPy as well as regular expressions. It is important to read the data fast and that the result is a NumPy array.
一个nastran文件如下所示:
A nastran file can look like the following:
GRID 1 3268.616-30.0828749.8656
GRID 2 3268.781 -3.-14749.8888
GRID 3 3422.488580.928382.49383
GRID 4 3422.488 10.-2.49383
...
我只对文件的右侧部分感兴趣.在那里,信息分别以x,y和z坐标为8个字符的块的形式出现.上面坐标的常见表示形式是
I am only interested in the right part of the file. There the information is present in chunks of 8 characters for the x, y and z coordinates respectively. A common representation of the coordinates above would be
3268.616, -30.0828, 749.8656
3268.781, -3.e-14, 749.8888
3422.488, 580.9283, 82.49383
3422.488, 10., -2.49383
到目前为止我尝试过的事情
到目前为止,我一直尝试使用正则表达式和NumPy来避免各种python for循环,以尽可能快地处理数据.将完整的文件读入内存并将其存储在fContent
变量中后,我尝试过:
What I tried so far
Up until now, I tried to use regular expressions and NumPy to avoid all kinds of python for loops to be as fast a possible about dealing with the data. After reading the complete file into memory and store it in the fContent
variable I tried:
vertices = np.array(re.findall("^.{24}(.{8})(.{8})(.{8})", fContent, re.MULTILINE), dtype=float)
但是,这对于-3.-14
表达式是不够的.一种解决方案是循环遍历正则表达式的结果字符串元组,并将所有.-
替换为.e-
,然后从字符串元组列表创建NumPy数组. (未在上面的代码中显示).但是,我认为这种方法会很慢,因为它涉及对所有找到的正则表达式元组进行循环并执行替换.
However, this falls short for the -3.-14
expressions. A solution would be to loop over the resulting string tuples of the regex and substitude all .-
with .e-
and then create the NumPy array from the list of string tuple. (Not shown in the code above). However, I think that this approach would be slow since it involves a loop over all found tuples of the regular expression and perform a substitution.
我正在寻找读取数据的任何快速方法.我目前的希望是能成功处理"-3.-14
"问题的智能正则表达式.如果.
不在8个字符块的末尾,则正则表达式需要将所有.-
字符替换为.e-
,但仅 .到目前为止,我还无法创建这样的正则表达式.但是正如我所说,任何其他快速读取数据的方式也非常受欢迎.
I am looking for any fast way to read in the data. My current hopes are on a smart regular expression that successfully deals with the "-3.-14
" problem. The regex would need to substitute all .-
characters with .e-
but only if the .
is not at the end of an 8 character block. Up until now, I was not able to create such a regular expression. But as I said, any other fast way of reading in the data is also very welcome.
推荐答案
这样的工作正常吗?匹配.-
并替换为.e-
.
Would something like this work fine? Match the .-
and replace with .e-
.
正则表达式:(\.-)(?!(.{7})?$)
这篇关于使用python读取nastran几何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!