问题描述
对于循环中的每个迭代,我都需要提供迭代次数"作为文件名,例如,目标是保存:
For every iteration in my loop for, I need to give 'the number of my iteration' as a name for the file, for example, the goal is to save:
my first iteration in the first file.
my second iteration in the second file.
....
我为此使用了numpy库,但是我的代码没有给我所需的解决方案,实际上,我的实际代码使我必须在每次迭代后输入文件名,如果我有6个,这很容易或7次迭代,但是在我有100次迭代的情况下,这没有意义:
I use for that the library numpy, but my code doesn't give me the solution that i need, in fact my actual code oblige me to enter the name of the file after each iteration, that is easy if I have 6 or 7 iteration, but i am in the case that I have 100 iteration, it doesn't make sense:
for line, a in enumerate(Plaintxt_file):
#instruction
#result
fileName = raw_input()
if(fileName!='end'):
fileName = r'C:\\Users\\My_resul\\Win_My_Scripts\\'+fileName
np.save(fileName+'.npy',Result)
ser.close()
如果您能帮助我,我将不胜感激.
I would be very grateful if you could help me.
推荐答案
从行号创建文件名:
for line, a in enumerate(Plaintxt_file):
fileName = r'C:\Users\My_resul\Win_My_Scripts\file_{}.npy'.format(line)
np.save(fileName, Result)
此操作以文件名file_0.npy
开头.如果要以1
开头,请在enumerate
中指定起始索引:
This start with file name file_0.npy
.If you like to start with 1
, specify the starting index in enumerate
:
for line, a in enumerate(Plaintxt_file, 1):
当然,这是假设您在其他任何地方都不需要line
以0
开头.
Of course, this assumes you don't need line
starting with 0
anywhere else.
这篇关于如何命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!