简体   繁体   中英

Setting a path for numpy.savetxt - file name contains loop variable

I am trying to save files with changing names into a different folder than the script is:

save_field( './cycle_1_580-846/txt/"frame_" + str( 580 + j ) + "_to_" + str( 581 + j ) + ".txt"', "Data68_0" + str( 580 + j ) + ".tif", str( 580 + j ) + "_to_" + str( 581 + j ), scale = 1500, width = 0.0025)

Now for saving the filename with loop variable in it I followed this post .

I naively thought that using ' and " will solve the problem, however, if I do that, I get a file in the right folder but with a wrong name (in this case: "frame_" + str( 580 + j ) + " to " + str( 581 + j ) + ".txt) (I would want: frame_580_to_581.txt). If I do not set the path, I have no problem.

Is there a clever way to overcome this ?

Cheers!

EDIT j is just a certain range of files (in this case it is from 0 to 270, incremented by 1 )

maybe this will also help

def save_field( filename, background, new_file, **kw):
""" Saves quiver plot of the data stored in the file
Parameters
----------
filename : string
the absolute path of the text file
background : string
the absolute path of the background image file
new_file : string
the name and format of the new file (png preferred)

Key arguments : (additional parameters, optional)
*scale*: [None | float]
*width*: [None | float]
"""

a = np.loadtxt(filename)
pl.figure()
bg = mpimg.imread(background)
imgplot = pl.imshow(bg, origin = 'lower', cmap = cmps.gray)
pl.hold(True)
invalid = a[:,3].astype('bool')

valid = ~invalid
pl.quiver(a[invalid,0],a[invalid,1],a[invalid,2],a[invalid,3],color='r',**kw)
pl.quiver(a[valid,0],a[valid,1],a[valid,2],a[valid,3],color='r',**kw)
v = [0, 256, 0, 126]
axis(v)
pl.draw()
pl.savefig(new_file, bbox_inches='tight')

I'm not certain what "j" is in your example... but I'll make some implicit assumptions and move forward from there (aka it looks like you are trying to build in an increment).

Try the following:

save_path = "./cycle_1_580-846/txt/frame_" 
save_file = (str(580) + "_to_" + str(581) + ".txt")
save_other_file = ("Data68_0" + str(580) + ".tif")

save_field((save_path + save_file), , save_other_file, (str(580) + "_to_" + str(581)), scale = 1500, width = 0.0025)

I recommend something like above - has some encapsulation, to me is a bit easier to read, and I'd even go further to cleanup the save_field()... but I don't know what it does and I didn't want to assume too much.

The REAL PROBLEM you are having, and you continue to use what you have, is that you are mixing your single-quotes with your double-quotes incorrectly.

sampleText = 'This is my "double quote" example'
sampleTextAgain = "This is my 'single quote' example"

Both of those are valid.

However, what you have is:

sampleBadText = '"I want to have some stuff added" + 580 + "together"'

The single-quote wrappings basically turn the ENTIRE line into a string. So yeah, it's no wonder why your file is named the way it is. You need to terminate your strings with the single/double quotes in the proper places and then jump back into python builtins/variable names, then back into strings in order to concat strings + variables + strings .

Not doing ALL the work for you, this is what it would start to look like:

save_field( './cycle_1_580-846/txt/frame_' + str( 580 + j ) + '_to_' + str( 581 + j ) + '.txt', [...]

Notice how I adjusted your single/double quotes so that way the "strings literals" are wrapped in single (or double) quotes, and then I properly terminate the string literal and concat (by way of + ) the variables/ints/additional strings...

Hope that makes sense, but you can go a long way to cleaning up what you got to avoid this type of confusion.

Ultimately, if you can get it to look like:

save_field(_file_name, _other_file_name, other_arg, scale=1500, width=0.0025)

That's MUCH cleaner and much more readable. But, again, I didn't spend the time to investigate what save_field does and what args and kwargs it accepts, so I have no idea if _file_name , _other_file_name , and other_arg even make sense as examples, I'm just hoping they do!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM