1

Is it possible to make the output filename a variable using np.savetxt? I have multiple input file from where I will read and perform some calculations and output the results in a file. Right now I am changing the file name each time for different output, But is there a way to do it automatically? The code I used is as below:

np.savetxt('ES-0.dat', np.c_[strain_percent, es_avg, es_std])

I would like to change the file name to ES-25.dat, ES 50.dat, ES-75.dat ....etc. This is also dependent upon the input file which I read like this:

flistC11 = glob.glob('ES-0')

Is there also a way to automatically change the input file to ES-25, ES-50, ES-75....etc?

I tried using loops but both the input and output has to be inside ' ' which does not allow me to make it a variable. Any idea how can I solve this problem? My work will be much easier then.

Added information after Saullo Castro's answer:

The file that I'm reading (ES*) consists two simple columns like this:

200 7.94
200 6.55
200 6.01
200 7.64
200 6.33
200 7.96
200 7.92

The whole script is as below:

import numpy as np
import glob
import sys


flistC11 = glob.glob('ES-s*')


#%strain

fdata4 = []
for fname in flistC11:
  load = np.loadtxt(fname)
fdata4.append(load[:,0]) #change to 0=strain or 1=%ES

fdata_arry4=np.array(fdata4)

print fdata_arry4

strain=np.mean(fdata_arry4[0,:])
strain_percent = strain/10

print strain_percent

#ES

fdata5 = []
for fname in flistC11:
  load = np.loadtxt(fname)
fdata5.append(load[:,1]) #change to 0=strain or 1=%ES

fdata_arry5=np.array(fdata5)

print fdata_arry5

es_avg=np.mean(fdata_arry5[0,:])
es_std=np.std(fdata_arry5[0,:])


print es_avg
print es_std

np.savetxt('{0}.dat'.format(fname), np.c_[strain_percent,es_avg,es_std])

2 Answers 2

1

You can do something like:

flistC11 = glob.glob('ES*')
for fname in flistC11:
    # ...something...
    np.savetxt('{0}.dat'.format(fname), np.c_[strain_percent, es_avg, es_std])

note that using ES* will tell glob() to return the names of all files beggining with ES.


EDIT: Based on your comments it seems you actually want something like this:

import glob

import numpy as np

flistC11 = glob.glob('ES-s*')
for fname in flistC11:
    strains, stresses = np.loadtxt(fname, unpack=True)
    strain = np.mean(strains)
    strain_percent = strain/10
    print fname, strain_percent
    es_avg = np.mean(stresses)
    es_std = np.std(stresses)
    print fname, es_avg, es_std
    np.savetxt('{0}.dat'.format(fname), np.c_[strain_percent, es_avg, es_std])
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you but its still not working, something going wrong in the indexing. I am getting "Invalid index" on line 15. flistC11 = glob.glob('ES-s*') #%strain fdata4 = [] for fname in flistC11: load = np.loadtxt(fname) fdata4 = [] fdata4.append(load[:,0]) #change to 0=strain or 1=%ES fdata_arry4=np.array(fdata4) print fdata_arry4 strain=np.mean(fdata_arry4[0,:]) strain_percent = strain/10 print strain_percent np.savetxt('{0}.dat'.format(fname), np.c_[strain_percent,es_avg,es_std])
Tried after getting rid of the fdata4=[] but still getting the same error!! Confused.
@Rafat it seems your error is due to another part of the code which is not related to this question... could you edit your question putting one small example of the input files you are reading?
@ Saullo Castro, just added more information in it. Thank you for your help.
Don't put multiple lines of code in a comment. If needed, add it to your original question.
|
1

It's not entirely clear where your error is (where is line 15?), but lets assume it is in the load. You have

fdata4 = []
for fname in flistC11:
  load = np.loadtxt(fname)
fdata4.append(load[:,0]) #change to 0=strain or 1=%ES

I'd suggest changing this to:

fdata4 = []
for fname in flistC11:
  print fname  # check that the names make sense
  load = np.loadtxt(fname)
  print load.shape  # check that the shape is as expected
  # maybe print more of 'load' here
  # I assume you want to collect 'load' from all files, not just the last
  fdata4.append(load[:,0]) #change to 0=strain or 1=%ES
print fdata4

In an Ipython shell, I had no problem producing:

In [90]: flistC11=['ES0','ES1','ES2']
In [91]: for fname in flistC11:
    np.savetxt('{}.dat'.format(fname), np.arange(10))
   ....:     
In [92]: glob.glob('ES*')
Out[92]: ['ES2.dat', 'ES0.dat', 'ES1.dat']

2 Comments

Thank you. I guess I fixed the secondary problem. There was a different file with similar name but with a different shape. But the main problem still is not solved. Using flistC11 = glob.glob('ES*') for fname in flistC11: # ...something... np.savetxt('{0}.dat'.format(fname), np.c_[strain_percent, es_avg, es_std]) only gives one output which is ES-s75 but not the others. This is really weird!
In your original code, savetxt is outside of a loop, and is executed only once using the last value of fname. In your comment code, it appears to be in a loop. I tried that, and it works.

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.