简体   繁体   English

Python正则表达式:从文本文件中提取关键字后的元组列表

[英]Python-Regular expressions: extract a list of tuples after a keyword from a text file

I want to implement a simplified version of what I have suggested here to import some vertices from an OpenFOAM blockMeshDict file and then visualize them with FreeCAD . 我想实现此处建议的简化版本,以便从OpenFOAM blockMeshDict文件导入一些顶点,然后使用FreeCAD对其进行可视化

the part of the file I'm interested in is a list of tuples (xi yi zi) s of floats, between parentheses after the vertices keyword. 我感兴趣的文件部分是vertices关键字后括号之间的浮点元组(xi yi zi)列表。 the file looks like this: 该文件如下所示:

vertices
(
    (1 2 3)
    (3 4 5)
    ...
)

I'm able to read the file from the same folder as the python script with: 我可以使用以下命令从与python脚本相同的文件夹中读取文件:

import os
os.chdir(os.path.dirname(__file__))
with open("blockMeshDict", "r") as f:
    s=f.read()

But then when I try to extract the content between the parentheses after the vertices with: 但是然后当我尝试使用以下命令在vertices之后的括号之间提取内容时:

import re
r1=re.search(r'vertices\n\((.*?)\)', s)
print r1.group(1)

I get the error: 我得到错误:

type 'exceptions.IndexError: no such group 输入'exceptions.IndexError:没有这样的组

and I don't know how to solve it. 而且我不知道如何解决。 What I want to have in the end is a list of tuples like [(x1,y1,z1),(x2,y2,z2)...] I would appreciate if you could help me know how I can implement this in Python 2.7. 我最后想要的是一个元组列表,例如[(x1,y1,z1),(x2,y2,z2)...]如果您能帮助我知道如何在Python中实现这一点,我将不胜感激。 2.7。

This will be the main regex to find the outter structure: \\bvertices\\s*\\((\\s*(?:\\([^)]+\\)\\s*)+)\\) 这将是查找\\bvertices\\s*\\((\\s*(?:\\([^)]+\\)\\s*)+)\\)结构的主要正则表达式: \\bvertices\\s*\\((\\s*(?:\\([^)]+\\)\\s*)+)\\)

Before that, we will remove all the comments. 在此之前,我们将删除所有评论。

And then an extra regex to extract all content inside the vertices structure: \\([^)]+\\) 然后是一个额外的正则表达式来提取顶点结构内的所有内容: \\([^)]+\\)

See demo here . 在此处查看演示。

The code: 编码:

import re

test_str = """
/*--------------------------------*- C++ -*----------------------------------*\
| =========                 |                                                 |
| \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox           |
|  \\    /   O peration     | Version:  5                                     |
|   \\  /    A nd           | Web:      www.OpenFOAM.org                      |
|    \\/     M anipulation  |                                                 |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version     2.0;
    format      ascii;
    class       dictionary;
    object      blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 0.001;

vertices
(
    (-20.6 0 -0.5)
    (-20.6 25.4 -0.5)  /* Some comment */
    (0 -25.4 -0.5)
    (0 0 -0.5)
    (0 25.4 -0.5)
    (206 -25.4 -0.5)
    (206 0 -0.5)
    (206 25.4 -0.5)
    (290 -16.6 -0.5)
    (290 0 -0.5)
    (290 16.6 -0.5)

    (-20.6 0 0.5)
    (-20.6 25.4 0.5)
    (0 -25.4 0.5)
    (0 0 0.5)
    (0 25.4 0.5)
    (206 -25.4 0.5)
    (206 0 0.5)
    (206 25.4 0.5)
    (290 -16.6 0.5)
    (290 0 0.5)
    (290 16.6 0.5)
  /*(1 2 3 4)*/ // Commented tuple
  //(1 2 3 4)
);

/* vertices commented
vertices
(
    (-20.6 0 -0.5)
    (-20.6 25.4 -0.5)
    (0 -25.4 -0.5)
    (0 0 -0.5)
    (0 25.4 -0.5)
    (206 -25.4 -0.5)
    (206 0 -0.5)
    (206 25.4 -0.5)
    (290 -16.6 -0.5)
    (290 0 -0.5)
    (290 16.6 -0.5)
)
*/

negY
(
    (2 4 1)
    (1 3 0.3)
);

posY
(
    (1 4 2)
    (2 3 4)
    (2 4 0.25)
);

posYR
(
    (2 1 1)
    (1 1 0.25)
);


blocks
(
    hex (0 3 4 1 11 14 15 12)
    (18 30 1)
    simpleGrading (0.5 $posY 1)

    hex (2 5 6 3 13 16 17 14)
    (180 27 1)
    edgeGrading (4 4 4 4 $negY 1 1 $negY 1 1 1 1)

    hex (3 6 7 4 14 17 18 15)
    (180 30 1)
    edgeGrading (4 4 4 4 $posY $posYR $posYR $posY 1 1 1 1)

    hex (5 8 9 6 16 19 20 17)
    (25 27 1)
    simpleGrading (2.5 1 1)

    hex (6 9 10 7 17 20 21 18)
    (25 30 1)
    simpleGrading (2.5 $posYR 1)
);

edges
(
);

boundary
(
    inlet
    {
        type patch;
        faces
        (
            (0 1 12 11)
        );
    }
    outlet
    {
        type patch;
        faces
        (
            (8 9 20 19)
            (9 10 21 20)
        );
    }
    upperWall
    {
        type wall;
        faces
        (
            (1 4 15 12)
            (4 7 18 15)
            (7 10 21 18)
        );
    }
    lowerWall
    {
        type wall;
        faces
        (
            (0 3 14 11)
            (3 2 13 14)
            (2 5 16 13)
            (5 8 19 16)
        );
    }
    frontAndBack
    {
        type empty;
        faces
        (
            (0 3 4 1)
            (2 5 6 3)
            (3 6 7 4)
            (5 8 9 6)
            (6 9 10 7)
            (11 14 15 12)
            (13 16 17 14)
            (14 17 18 15)
            (16 19 20 17)
            (17 20 21 18)
        );
    }
);
// ************************************************************************* //
"""

# Clean comments:
test_str = re.sub(r"//.*", '', test_str)
test_str = re.sub(r"/\*.*?\*/", '', test_str, 0, re.DOTALL)

# Match main group
matches = re.findall(r"\bvertices\s*\((\s*(?:\([^)]+\)\s*)+)\)", test_str, re.MULTILINE | re.DOTALL)

# Fetch tuples
matches2 = re.findall(r"\([^)]+\)", matches[0], re.MULTILINE | re.DOTALL)
print matches2

Explained: 解释:

\b        # word boundary
vertices  # literal 'vertices'
\s*       # 0 or more spaces (includes line feed/carriage return)
\(        # literal '('
  (       # First capturing group
    \s*   # Som spaces
    (?:   # Group
       \([^)]+\)   # literal '(' + any non-')' character 1 or more times + literal ')'
       \s*         # extra spaces
    )+    # repeated one or more times
  )
\)        # literal ')'

Then you get that captured group and search for \\([^)]+\\) . 然后,获取该捕获的组并搜索\\([^)]+\\) That will find instances of vertices. 那将找到顶点的实例。

vertices
(
    (1 2 3)
    (3 4 5)
    ...
)

add a re.DOTALL as in 添加一个re.DOTALL

r1 = re.search(r'(?:vertices\s+)?(\([\w\s]+\))', s, re.DOTALL)
print r1.group(1)

>>> (1 2 3)

you may need to use re.findall if you want all results stored as a list as in 如果要将所有结果存储为列表,则可能需要使用re.findall

r1 = re.findall(r'(?:vertices\s+)?(\([\w\s]+\))', s, re.DOTALL)
print r1

>>> ['(1 2 3)', '(3 4 5)']

My test data file blockMeshDict : 我的测试数据文件blockMeshDict

vertices // comment 1
(
          (1 2 3) // comment 2
/* :) */  (3 4 5) /* multi line...
...comment */

          (65.71 72.8 2.0)
)

Code: 码:

import re

with open("blockMeshDict", "r") as f:
    s=f.read()

# Remove comments like "//" until end of line
s = re.sub(r'//.*', '', s)

# Remove comments between /* and */
s = re.sub(r'/\*(.|\s)*?\*/', '', s, re.DOTALL)

r1 = re.search(r'vertices\s*\(\s*(.*)\s*\)', s, re.DOTALL)
vertices = [(float(v[0]),float(v[1]),float(v[2]))
        for v in re.findall(r'\(\s*([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s*\)', r1.group(1))]

print(vertices)

Output is the list of tuples (of float's): 输出是(浮点数的)元组列表:

[(1.0, 2.0, 3.0), (3.0, 4.0, 5.0), (65.71, 72.8, 2.0)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM