简体   繁体   English

在python中从具有不同行和列大小的文本文件中读取值

[英]Reading values from a text file with different row and column size in python

I have read other simliar posts but they don't seem to work in my case. 我读过其他类似的文章,但在我看来它们似乎不起作用。 Hence, I'm posting it newly here. 因此,我在这里新发布了它。

I have a text file which has varying row and column sizes. 我有一个文本文件,其中的行和列大小都不同。 I am interested in the rows of values which have a specific parameter. 我对具有特定参数的值行感兴趣。 Eg in the sample text file below, I want the last two values of each line which has the number '1' in the second position. 例如,在下面的示例文本文件中,我希望每行的最后两个值在第二个位置具有数字“ 1”。 That is, I want the values '1, 101', '101, 2', '2, 102' and '102, 3' from the lines starting with the values '101 to 104' because they have the number '1' in the second position. 也就是说,我希望从值“ 101至104”开始的行中使用值“ 1、101”,“ 101、2”,“ 2、102”和“ 102、3”,因为它们具有数字“ 1”在第二位置。

$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

The problem is, with the code I have come up with mentioned below, it starts from '101' but it reads the values from the other lines upto '304' or more. 问题是,随着我下面提到的代码的出现,它从“ 101”开始,但是从其他行读取的值直到“ 304”或更多。 What am I doing wrong or does someone has a better way to tackle this? 我做错了什么,或者有人可以更好地解决这个问题?

# 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: The piece of code used to find the parameters like two_noded_elem_start is as follows: *编辑:用于查找诸如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()
>>> 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

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

相关问题 如何使用Python中的行和列数据从文本文件读取值 - How to read values from text file, with row and column data in Python 从文件python读取时的不同文本 - Different text when reading from file python 从 Python 中的文本文件中读取和写入不同语言的文本 - Reading and writing text in different languages from a text file in Python 从一列 Python 中包含多个值的 .CSV 文件中读取 - Reading from a .CSV file that contains multiple values in one column Python Python:读取文件并向不同行的字典添加键和值 - Python: Reading a file and adding keys and values to dictionaries from different lines 从python中的文本文件中读取列数据(自定义空格作为分隔符) - Reading column data from a text file in python (custom spaces as delimiter) 使用 python os.walk 从不同目录读取文本文件 - Reading text file from different directories with python os.walk 将float从两列文本文件读取到Python中的数组时出错 - error with reading float from two column text file into an array in Python 从文本文件中读取值后,如果条件未在python中执行 - if condition does not executed in python after reading values from text file 从python中的文本文件中读取逗号分隔值 - Reading comma separated values from text file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM