从UNV文件读取数据

huangapple go评论62阅读模式
英文:

Reading data from a UNV file

问题

我编写了一个Python代码来从UNV文件中提取数据。我遇到了越界错误问题。
我需要提取边界组数据(从第1270行到第1357行)。错误如下所示。
有2个边界组:SOURCE和WALLS:在UNV文件中,每个边界组将有2个块:1. 第一部分是边界面数据,2. 第二部分是边界节点数据(我想要提取这些数据)。我想要提取的这个节点组数据位于第1270行到第1357行。
UNV文件链接:https://www.dropbox.com/scl/fi/idnryyzhumr53tfrzvjkb/Mesh_5x5x5x.unv?rlkey=h8ck16r90d5u2wxoy7hljfjvs&dl=0

以下是代码的一部分:

from collections import defaultdict
import numpy as np
from tqdm import tqdm
import os, sys
import math

# 这是UNV文件
input = open('Mesh_5x5x5x.unv','r')
output = open('co-ordinates.txt','w')
output2 = open('nodes.txt','w')
output3 = open('connectivity.txt','w')
output4 = open('boundary_2_6.txt','w')
data = input.readlines()
# 其余代码...

输出:

p= ['7', '14', '0', '0', '7', '15', '0', '0'] 8
p= ['7', '16', '0', '0', '7', '7', '0', '0'] 8
p= ['7', '29', '0', '0', '7', '30', '0', '0'] 8
p= ['7', '31', '0', '0', '7', '32', '0', '0'] 8
p= ['7', '1', '0', '0', '7', '5', '0', '0'] 8
p= ['-1'] 1
Traceback (most recent call last):

  File ~\anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\jnanadeepti\prasad\coding2023\lbm-pythondd-flow\readunv\read_unv-test.py:88
    p = data[bnd_index[l]+1+k].split()

IndexError: list index out of range

希望这有助于解决你的问题。如果你有任何其他问题,请随时提出。

英文:

I wrote a Python code to extract Data from UNV file. I am facing out of bound error Issues.
I need to extract the Boundary Groups data (From lines: 1270 to 1357). The error is given below.
There are 2 Boundary groups: SOURCE AND WALLS: In the UNV file, there will be 2 blocks of each Boundary group: 1. The first part is Boundary face data, 2. The second part is Boundary NODE data (I want this data to be extracted). This group data of Node which I wanted to extract is from Lines 1270 to 1357.
UNV File Link: https://www.dropbox.com/scl/fi/idnryyzhumr53tfrzvjkb/Mesh_5x5x5x.unv?rlkey=h8ck16r90d5u2wxoy7hljfjvs&dl=0

    from collections import defaultdict
import numpy as np
from tqdm import tqdm
import os, sys
import math
** # This is the UNV file**
input = open('Mesh_5x5x5x.unv','r')
output = open('co-ordinates.txt','w')
output2 = open('nodes.txt','w')
output3 = open('connectivity.txt','w')
output4 = open('boundary_2_6.txt','w')
data = input.readlines()
print(len(data))
input.seek(0)
x= []
y=[]
z=[]
node=[]
conn1 = []
conn2 = []
conn3 = []
conn4 = []
boundary = 0
boundary_list = []
boundary_names = defaultdict(list)
boundary_names_2_5 = defaultdict(list)
boundary_index = []
**READING THE DATA FROM UNV FILE**
for line in data:
if line.strip().lstrip() == '2411':
start_index = data.index(line)
if line.strip().lstrip() == '2412':
stop_index = data.index(line)
**READIG THE COORDINATE DATA FROM UNV FILE**
for i in range(start_index+1,stop_index-2,2):
l1 = data[i].strip('\n \t').lstrip().split()
node.append(l1[0])
l2 = data[i+1].strip('\n \t').lstrip().split()
x.append(l2[0])
y.append(l2[1])
z.append(l2[2])
#print (l1)
#print (l2)
** # READING CONNECTIVITY OF FACES**
for line in data:
if line.strip().lstrip() == '2412':
start_index = data.index(line)
if line.strip().lstrip() == '2467':
stop_index = data.index(line)
for i in range(start_index+1,stop_index,1):
if len(data[i].strip().lstrip().split())==4:
conn1.append(data[i].strip().lstrip().split()[0])
conn2.append(data[i].strip().lstrip().split()[1])
conn3.append(data[i].strip().lstrip().split()[2])
conn4.append(data[i].strip().lstrip().split()[3])
**# READ BUNDARY INFORMATION: ELEMENTS AND NODES:**
**HERE I AM PAERICULARLY INTERESTED IN EXTRACTING NODE DATA WHICH IS CONTAINED IN THE LAST GROUPS : SOURCE **AND WALLS (LINES 1270 TO 1357)**
for i in range(stop_index+1,len(data),1):
#print('DDD:',i,stop_index+1) 
if data[i].strip().lstrip()[0].isalpha() == True:
print('DDD:',i,stop_index+1,len(data))
boundary_names[data[i].strip().lstrip()]
boundary_names_2_5[data[i].strip().lstrip()]
#exec(data[i].strip().lstrip()+'=[]')
#exec(data[i].strip().lstrip()+'_2_5=[]')
boundary = boundary+0.5
boundary_list.append(data[i].strip().lstrip())
boundary_index.append(i)
print(boundary_list)
print(boundary_index)
print(len(boundary_index))
bnd_index = boundary_index[len(boundary_index)//2:]
bnd = len(bnd_index)
#bnd = 4
print('bnd',bnd)
#
for l in range(bnd):
print(l)
print(boundary_index[l+2])
print(boundary_index[l+1])
lines = (boundary_index[l+3]-boundary_index[l])-3
print("lines=",lines)
for k in range(lines):
#print("k=",k)
#print("l+1+k=",l+1+k)
p = data[bnd_index[l]+1+k].split()
print("p=",p, len(p))
#for j in range(len(p)):
#if float(p[j])>-0.001:
#boundary_names[data[bnd_index[l]].strip()].append(p[j])
#exec(data[bnd_index[l]].strip()+'.append(p[j])')
if (len(p)>1):
p = data[bnd_index[l]+1+k].split()
boundary_names_2_5[data[bnd_index[l]].strip()].append(p[1])
boundary_names_2_5[data[bnd_index[l]].strip()].append(p[5])
#exec(data[bnd_index[l]].strip()+'_2_5.append(p[1])')
#exec(data[bnd_index[l]].strip()+'_2_5.append(p[5])')
```
**OUTPUT:**
**p= ['7', '14', '0', '0', '7', '15', '0', '0'] 8
p= ['7', '16', '0', '0', '7', '7', '0', '0'] 8
p= ['7', '29', '0', '0', '7', '30', '0', '0'] 8
p= ['7', '31', '0', '0', '7', '32', '0', '0'] 8
p= ['7', '1', '0', '0', '7', '5', '0', '0'] 8
p= ['-1'] 1
Traceback (most recent call last):
File ~\anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
exec(code, globals, locals)
File c:\jnanadeepti\prasad\coding2023\lbm-python\3d\3d-flow\readunv\read_unv-test.py:88
p = data[bnd_index[l]+1+k].split()
IndexError: list index out of range**
`
</details>
# 答案1
**得分**: 1
这段代码可以轻松提取所有“SOURCE”和“WALLS”部分的数据。如果你只想保留第二个“SOURCE/WALLS”部分,只需在`state = 1`后设置`source = []`,在`state = 2`后设置`walls = []`。
```python
from pprint import pprint
f = open('../Downloads/Mesh_5x5x5x.unv', 'r')
state = 0
source = []
walls = []
for line in f:
line = line.strip()
if line == 'SOURCE':
state = 1
elif line == 'WALLS':
state = 2
elif line == '-1':
state = 0
elif state == 1:
source.append([int(i) for i in line.split()])
elif state == 2:
walls.append([int(i) for i in line.split()])
pprint(source)
pprint(walls)
```
输出结果已包括在你提供的内容中。
<details>
<summary>英文:</summary>
This extracts all the data from all of the SOURCE and WALLS section in an easy way.  Perhaps you can modify this to get what you need.  You&#39;re going to a lot of totally unnecessary work.
If you only want to keep the second SOURCE/WALLS sections, just set `source = []` after `state = 1`, and `walls = []` after `state = 2`.
```
from pprint import pprint
f = open(&#39;../Downloads/Mesh_5x5x5x.unv&#39;, &#39;r&#39;)
state = 0
source = []
walls = []
for line in f:
line = line.strip()
if line == &#39;SOURCE&#39;:
state = 1
elif line == &#39;WALLS&#39;:
state = 2
elif line == &#39;-1&#39;:
state = 0
elif state == 1:
source.append([int(i) for i in line.split()])
elif state == 2:
walls.append([int(i) for i in line.split()])
pprint(source)
pprint(walls)        
```
Output:
```
[[8, 186, 0, 0, 8, 187, 0, 0],
[8, 188, 0, 0, 8, 189, 0, 0],
[8, 190, 0, 0, 8, 191, 0, 0],
[8, 192, 0, 0, 8, 193, 0, 0],
[8, 194, 0, 0, 8, 195, 0, 0],
[8, 196, 0, 0, 8, 197, 0, 0],
[8, 198, 0, 0, 8, 199, 0, 0],
[8, 200, 0, 0, 8, 201, 0, 0],
[8, 202, 0, 0, 8, 203, 0, 0],
[8, 204, 0, 0, 8, 205, 0, 0],
[8, 206, 0, 0, 8, 207, 0, 0],
[8, 208, 0, 0, 8, 209, 0, 0],
[8, 210, 0, 0],
[2, 0, 0, 0, 0, 0, 0, 125],
[7, 3, 0, 0, 7, 53, 0, 0],
[7, 54, 0, 0, 7, 55, 0, 0],
[7, 56, 0, 0, 7, 45, 0, 0],
[7, 46, 0, 0, 7, 47, 0, 0],
[7, 48, 0, 0, 7, 13, 0, 0],
[7, 14, 0, 0, 7, 15, 0, 0],
[7, 16, 0, 0, 7, 7, 0, 0],
[7, 137, 0, 0, 7, 138, 0, 0],
[7, 139, 0, 0, 7, 140, 0, 0],
[7, 141, 0, 0, 7, 142, 0, 0],
[7, 143, 0, 0, 7, 144, 0, 0],
[7, 145, 0, 0, 7, 146, 0, 0],
[7, 147, 0, 0, 7, 148, 0, 0],
[7, 149, 0, 0, 7, 150, 0, 0],
[7, 151, 0, 0, 7, 152, 0, 0],
[7, 29, 0, 0, 7, 30, 0, 0],
[7, 31, 0, 0, 7, 32, 0, 0],
[7, 1, 0, 0, 7, 5, 0, 0],
[6, 0, 0, 0, 0, 0, 0, 136]]
[[8, 111, 0, 0, 8, 112, 0, 0],
[8, 113, 0, 0, 8, 114, 0, 0],
[8, 115, 0, 0, 8, 116, 0, 0],
[8, 117, 0, 0, 8, 118, 0, 0],
[8, 119, 0, 0, 8, 120, 0, 0],
[8, 121, 0, 0, 8, 122, 0, 0],
[8, 123, 0, 0, 8, 124, 0, 0],
[8, 125, 0, 0, 8, 126, 0, 0],
[8, 127, 0, 0, 8, 128, 0, 0],
[8, 129, 0, 0, 8, 130, 0, 0],
[8, 131, 0, 0, 8, 132, 0, 0],
[8, 133, 0, 0, 8, 134, 0, 0],
[8, 135, 0, 0, 8, 136, 0, 0],
[8, 137, 0, 0, 8, 138, 0, 0],
[8, 139, 0, 0, 8, 140, 0, 0],
[8, 141, 0, 0, 8, 142, 0, 0],
[8, 143, 0, 0, 8, 144, 0, 0],
[8, 145, 0, 0, 8, 146, 0, 0],
[8, 147, 0, 0, 8, 148, 0, 0],
[8, 149, 0, 0, 8, 150, 0, 0],
[8, 151, 0, 0, 8, 152, 0, 0],
[8, 153, 0, 0, 8, 154, 0, 0],
[8, 155, 0, 0, 8, 156, 0, 0],
[8, 157, 0, 0, 8, 158, 0, 0],
[8, 159, 0, 0, 8, 160, 0, 0],
[8, 161, 0, 0, 8, 162, 0, 0],
[8, 163, 0, 0, 8, 164, 0, 0],
[8, 165, 0, 0, 8, 166, 0, 0],
[8, 167, 0, 0, 8, 168, 0, 0],
[8, 169, 0, 0, 8, 170, 0, 0],
[8, 171, 0, 0, 8, 172, 0, 0],
[8, 173, 0, 0, 8, 174, 0, 0],
[8, 175, 0, 0, 8, 176, 0, 0],
[8, 177, 0, 0, 8, 178, 0, 0],
[8, 179, 0, 0, 8, 180, 0, 0],
[8, 181, 0, 0, 8, 182, 0, 0],
[8, 183, 0, 0, 8, 184, 0, 0],
[8, 185, 0, 0, 8, 61, 0, 0],
[8, 62, 0, 0, 8, 63, 0, 0],
[8, 64, 0, 0, 8, 65, 0, 0],
[8, 66, 0, 0, 8, 67, 0, 0],
[8, 68, 0, 0, 8, 69, 0, 0],
[8, 70, 0, 0, 8, 71, 0, 0],
[8, 72, 0, 0, 8, 73, 0, 0],
[8, 74, 0, 0, 8, 75, 0, 0],
[8, 76, 0, 0, 8, 77, 0, 0],
[8, 78, 0, 0, 8, 79, 0, 0],
[8, 80, 0, 0, 8, 81, 0, 0],
[8, 82, 0, 0, 8, 83, 0, 0],
[8, 84, 0, 0, 8, 85, 0, 0],
[8, 86, 0, 0, 8, 87, 0, 0],
[8, 88, 0, 0, 8, 89, 0, 0],
[8, 90, 0, 0, 8, 91, 0, 0],
[8, 92, 0, 0, 8, 93, 0, 0],
[8, 94, 0, 0, 8, 95, 0, 0],
[8, 96, 0, 0, 8, 97, 0, 0],
[8, 98, 0, 0, 8, 99, 0, 0],
[8, 100, 0, 0, 8, 101, 0, 0],
[8, 102, 0, 0, 8, 103, 0, 0],
[8, 104, 0, 0, 8, 105, 0, 0],
[8, 106, 0, 0, 8, 107, 0, 0],
[8, 108, 0, 0, 8, 109, 0, 0],
[8, 110, 0, 0],
[5, 0, 0, 0, 0, 0, 0, 36],
[7, 25, 0, 0, 7, 26, 0, 0],
[7, 27, 0, 0, 7, 28, 0, 0],
[7, 89, 0, 0, 7, 90, 0, 0],
[7, 91, 0, 0, 7, 92, 0, 0],
[7, 93, 0, 0, 7, 94, 0, 0],
[7, 95, 0, 0, 7, 96, 0, 0],
[7, 97, 0, 0, 7, 98, 0, 0],
[7, 99, 0, 0, 7, 100, 0, 0],
[7, 101, 0, 0, 7, 102, 0, 0],
[7, 103, 0, 0, 7, 104, 0, 0],
[7, 49, 0, 0, 7, 50, 0, 0],
[7, 51, 0, 0, 7, 52, 0, 0],
[7, 6, 0, 0, 7, 105, 0, 0],
[7, 106, 0, 0, 7, 107, 0, 0],
[7, 108, 0, 0, 7, 109, 0, 0],
[7, 110, 0, 0, 7, 111, 0, 0],
[7, 112, 0, 0, 7, 113, 0, 0],
[7, 114, 0, 0, 7, 115, 0, 0],
[7, 116, 0, 0, 7, 117, 0, 0],
[7, 118, 0, 0, 7, 119, 0, 0],
[7, 120, 0, 0, 7, 33, 0, 0],
[7, 34, 0, 0, 7, 35, 0, 0],
[7, 36, 0, 0, 7, 121, 0, 0],
[7, 122, 0, 0, 7, 123, 0, 0],
[7, 124, 0, 0, 7, 125, 0, 0],
[7, 126, 0, 0, 7, 127, 0, 0],
[7, 128, 0, 0, 7, 129, 0, 0],
[7, 130, 0, 0, 7, 131, 0, 0],
[7, 132, 0, 0, 7, 133, 0, 0],
[7, 134, 0, 0, 7, 135, 0, 0],
[7, 136, 0, 0, 7, 21, 0, 0],
[7, 22, 0, 0, 7, 23, 0, 0],
[7, 24, 0, 0, 7, 41, 0, 0],
[7, 42, 0, 0, 7, 43, 0, 0],
[7, 44, 0, 0, 7, 9, 0, 0],
[7, 10, 0, 0, 7, 11, 0, 0],
[7, 12, 0, 0, 7, 2, 0, 0],
[7, 8, 0, 0, 7, 37, 0, 0],
[7, 38, 0, 0, 7, 39, 0, 0],
[7, 40, 0, 0, 7, 4, 0, 0],
[7, 17, 0, 0, 7, 18, 0, 0],
[7, 19, 0, 0, 7, 20, 0, 0],
[7, 3, 0, 0, 7, 53, 0, 0],
[7, 54, 0, 0, 7, 55, 0, 0],
[7, 56, 0, 0, 7, 45, 0, 0],
[7, 46, 0, 0, 7, 47, 0, 0],
[7, 48, 0, 0, 7, 57, 0, 0],
[7, 58, 0, 0, 7, 59, 0, 0],
[7, 60, 0, 0, 7, 61, 0, 0],
[7, 62, 0, 0, 7, 63, 0, 0],
[7, 64, 0, 0, 7, 65, 0, 0],
[7, 66, 0, 0, 7, 67, 0, 0],
[7, 68, 0, 0, 7, 69, 0, 0],
[7, 70, 0, 0, 7, 71, 0, 0],
[7, 72, 0, 0, 7, 73, 0, 0],
[7, 74, 0, 0, 7, 75, 0, 0],
[7, 76, 0, 0, 7, 77, 0, 0],
[7, 78, 0, 0, 7, 79, 0, 0],
[7, 80, 0, 0, 7, 81, 0, 0],
[7, 82, 0, 0, 7, 83, 0, 0],
[7, 84, 0, 0, 7, 85, 0, 0],
[7, 86, 0, 0, 7, 87, 0, 0],
[7, 88, 0, 0, 7, 13, 0, 0],
[7, 14, 0, 0, 7, 15, 0, 0],
[7, 16, 0, 0, 7, 7, 0, 0],
[7, 29, 0, 0, 7, 30, 0, 0],
[7, 31, 0, 0, 7, 32, 0, 0],
[7, 1, 0, 0, 7, 5, 0, 0]]
```
</details>

huangapple
  • 本文由 发表于 2023年7月28日 04:24:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定