2016-10-22 8 views
0

私は他のsimliar投稿を読みましたが、私の場合は動作しません。したがって、私はここにそれを新たに掲載しています。Pythonで行と列のサイズが異なるテキストファイルから値を読み取る

私はさまざまな行と列のサイズを持つテキストファイルを持っています。私は特定のパラメータを持つ値の行に興味があります。例えば。以下のサンプルテキストファイルでは、第2の位置に数字「1」を持つ各行の最後の2つの値が必要です。つまり、数値「101」から「104」までの行から「1」、「101」、「101」、「2」、「102」、「102、第2の位置にある。

$MeshFormat 
2.2 0 8 
$EndMeshFormat 
$Nodes 
425 
. 
. 
$EndNodes 
$Elements 
630 
. 
97 15 2 0 193 97 
98 15 2 0 195 98 
99 15 2 0 197 99 
100 15 2 0 199 100 
101 1 2 0 201 1 101 
102 1 2 0 201 101 2 
103 1 2 0 202 2 102 
104 1 2 0 202 102 3 
301 2 2 0 303 178 78 250 
302 2 2 0 303 250 79 178 
303 2 2 0 303 198 98 249 
304 2 2 0 303 249 99 198 
. 
. 
. 
$EndElements 

問題は、私は下記が出ているコードと、それは「101」から始まるが、他の点で最大のライン「304」以上の値を読み取り、です。私は間違って何をしているのですか、誰かがこれに取り組むためのよりよい方法を持っていますか?

# Here, (additional_lines + anz_knoten_gmsh - 2) are additional lines that need to be skipped 
# at the beginning of the .txt file. Initially I find out where the range 
# of the lines lies which I need. 
# The two_noded_elem_start is the first line having the '1' at the second position 
# and four_noded_elem_start is the first line number having '2' in the second position. 
# So, basically I'm reading between these two parameters. 


input_file = open(os.path.join(gmsh_path, "mesh_outer_region.msh")) 
output_file = open(os.path.join(gmsh_path, "mesh_skip_nodes.txt"), "w") 

for i, line in enumerate(input_file):             
    if i == (additional_lines + anz_knoten_gmsh + two_noded_elem_start - 2):   
     break 

for i, line in enumerate(input_file):            
    if i == additional_lines + anz_knoten_gmsh + four_noded_elem_start - 2:   
     break 

    elem_list = line.strip().split()     
    del elem_list[:5]        
    writer = csv.writer(output_file)    
    writer.writerow(elem_list)      

input_file.close() 
output_file.close() 

* EDIT:次のようにtwo_noded_elem_startのようなパラメータを見つけるために使用されるコードの一部は次のとおりです。

# anz_elemente_ueberg_gmsh is another parameter that is found out 
# from a previous piece of code and '$EndElements' is what 
# is at the end of the text file "mesh_outer_region.msh". 

input_file = open(os.path.join(gmsh_path, "mesh_outer_region.msh"), "r") 
for i, line in enumerate(input_file):      
    if line.strip() == anz_elemente_ueberg_gmsh: 
     break 

for i, line in enumerate(input_file):      
    if line.strip() == '$EndElements':      
     break 

    element_list = line.strip().split()     
    if element_list[1] == '1':        


     two_noded_elem_start = element_list[0]      
     two_noded_elem_start = int(two_noded_elem_start)    
     break 
input_file.close() 
+0

これは、101から始まるあなたは確かにいますか?前の行をスキップするコードはどこにありますか?このコードはあなたが説明したものに似ていません。 – zvone

+0

はい、最初の行は '1'で始まり、101で始まります。他のコードは投稿しています。 –

答えて

1
>>> with open('filename') as fh:    # Open the file 
... for line in fh:      # For each line the file 
...  values = line.split()    # Split the values into a list 
...  if values[1] == '1':    # Compare the second value 
...   print values[-2], values[-1] # Print the 2nd from last and last 
1 101 
101 2 
2 102 
102 3 
+0

お返事ありがとうございました。しかし、編集した.txtファイルをご覧ください。最初の数行はリストのインデックス '1'に値を持たないので、あなたが言及したように行を読むことはできません。だから、私は '列挙しなければならないだろう 'と推測している –

関連する問題