Go through values in a list and put them into a variable in Python

0

I have a list with data that I want to go through with a for loop and put that data in a variable.

I am using the following code:

from Read_element_IDs import elementID
element_list = elementID()
#Comprueba importación de la lista print element_list
input_file_path = "A350-900_SMM10_I4_Exceedance_LCs_Step_7_FS_LHS.f06"
output_file_path = "EXTRACTION_FORCES_CRODS.txt"
with open(input_file_path, "r") as in_file, \
     open(output_file_path, "w") as out_file:
    i = 0
    for i in element_list:
        element_id = element_list[i]
        for line in in_file:
        #element_id = "55100515"
            if line[:26] == "      ELEMENT-ID =" + element_id:
                out_file.write(line)
                for line in in_file:
                    if line[0] == "1":
                        break
                    out_file.write(line)
                break
    else:
        print("Element was not found")

I get an error in element_id = element_list [i]     element_id = element_list [i] TypeError: list indices must be integers, not str

I do not understand why he is not taking i as a whole

How do I resolve it?

    
asked by Manuel Jurado 10.04.2018 в 12:05
source

1 answer

0

The error as commented @abulafia is that when you iterate with a for in on an iterable this already returns each item:

for element_id in element_list:
    ...

However, your code will not work as expected because in practice you may end up looking for only for the first item in the list . This is because you iterate over a file and in the first iteration (first id) and you cross the whole file if the line is not found, in the following id for line in in_file will not return any line of the file, since the cursor It is at the end of this one. If it finds it, in the next search it will not take into account the piece of file before the block found from the previous id in the search for the new id, and so on.

If all the matches go to the same output file, the simplest thing is to do without the for that iterates over the ids and modify the conditional to check if there is a line of the form: " "ELEMENT-ID = {algún id de la lista}" :

from Read_element_IDs import elementID


element_set = elementID()

#Comprueba importación de la lista print element_list
input_file_path = "A350-900_SMM10_I4_Exceedance_LCs_Step_7_FS_LHS.f06"
output_file_path = "EXTRACTION_FORCES_CRODS.txt"

with open(input_file_path, "r") as in_file, \
     open(output_file_path, "w") as out_file:
        for line in in_file:
            if line[:18] == "      ELEMENT-ID =" and line[18:].strip() in element_set:
                out_file.write(line)
                for line in in_file:
                    if line[0] == "1":
                        break
                    out_file.write(line)

It is also necessary to remove the last break if you want more than one block to be captured.

If you do not care about the order in which the ids are searched, it is more efficient to use a set (set) than a list:

element_list = set(elementID())
    
answered by 10.04.2018 в 12:41