0

I am trying to read in files with python, extract a list of values and convert those values to an array so I will get 1 separate array for each file read.

I have tried the .append function however this seems to only add the subsequent lists to the first array created e.g. array 0 = list 0, array 1 = list 0 + list 1

d={}
tup=0
energy =[]
for i in range(len(file_path)):     #iterate for i = n number of files
    filename = os.path.basename(file_path[tup])
    s=open(filename, 'r')
    paths =os.path.basename(''.join(file_path[tup])) #sets path name as filename
    s=open(filename, 'r')
    f1 = s.readlines()           #reads lines in open file  
    s.close()                    #closes file    
    for z, line in enumerate(f1):             
        if "cell  1" in line:      #searches for phrase 'cell 1' in file 
            for x in f1[z+2:z+1004]:     #copies values in target range
                energy.append((float((x.split(None, 1)[0]))))     #appends only first values from target range to energy list            
    d['arrayx{0}'.format(tup)]= numpy.array(energy)

    tup +=1
print('array 0 ', d['arrayx0'])
print('array 1 ', d['arrayx1']) 
print('array 2 ', d['arrayx2'])  

let's say the extracted values for 3 files are the following:

A = [1,2,3]
B = [4,5,6]
C = [7,8,9]

I would expect the output from each print statement to be the following:

array 0 [1,2,3]
array 1 [4,5,6]
array 2 [7,8,9]

instead what I actually get is:

array 0 [1,2,3]
array 1 [1,2,3,4,5,6]
array 2 [1,2,3,4,5,6,7,8,9]

1 Answer 1

1

You should move the initialization of energy into the main loop:

for i in range(len(file_path)):     #iterate for i = n number of files
   energy =[]
   ...

Otherwise energy keeps its value from one iteration to the next, giving the result you observe.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.