![](/img/trans.png)
[英]How to extract text part from file using Python & Regular Expressions
[英]Python-Regular expressions: extract a list of tuples after a keyword from a text file
我想實現此處建議的簡化版本,以便從OpenFOAM blockMeshDict
文件導入一些頂點,然后使用FreeCAD對其進行可視化 。
我感興趣的文件部分是vertices
關鍵字后括號之間的浮點元組(xi yi zi)
列表。 該文件如下所示:
vertices
(
(1 2 3)
(3 4 5)
...
)
我可以使用以下命令從與python腳本相同的文件夾中讀取文件:
import os
os.chdir(os.path.dirname(__file__))
with open("blockMeshDict", "r") as f:
s=f.read()
但是然后當我嘗試使用以下命令在vertices
之后的括號之間提取內容時:
import re
r1=re.search(r'vertices\n\((.*?)\)', s)
print r1.group(1)
我得到錯誤:
輸入'exceptions.IndexError:沒有這樣的組
而且我不知道如何解決。 我最后想要的是一個元組列表,例如[(x1,y1,z1),(x2,y2,z2)...]
如果您能幫助我知道如何在Python中實現這一點,我將不勝感激。 2.7。
這將是查找\\bvertices\\s*\\((\\s*(?:\\([^)]+\\)\\s*)+)\\)
結構的主要正則表達式: \\bvertices\\s*\\((\\s*(?:\\([^)]+\\)\\s*)+)\\)
在此之前,我們將刪除所有評論。
然后是一個額外的正則表達式來提取頂點結構內的所有內容: \\([^)]+\\)
在此處查看演示。
編碼:
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
解釋:
\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 ')'
然后,獲取該捕獲的組並搜索\\([^)]+\\)
。 那將找到頂點的實例。
vertices
(
(1 2 3)
(3 4 5)
...
)
添加一個re.DOTALL
r1 = re.search(r'(?:vertices\s+)?(\([\w\s]+\))', s, re.DOTALL)
print r1.group(1)
>>> (1 2 3)
如果要將所有結果存儲為列表,則可能需要使用re.findall 。
r1 = re.findall(r'(?:vertices\s+)?(\([\w\s]+\))', s, re.DOTALL)
print r1
>>> ['(1 2 3)', '(3 4 5)']
我的測試數據文件blockMeshDict
:
vertices // comment 1
(
(1 2 3) // comment 2
/* :) */ (3 4 5) /* multi line...
...comment */
(65.71 72.8 2.0)
)
碼:
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)
輸出是(浮點數的)元組列表:
[(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.