Reputation: 137
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])
Upvotes: 1
Views: 2278
Reputation: 58915
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])
Upvotes: 1
Reputation: 231385
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']
Upvotes: 1