英文:
Saving filename in a specific format in Python
问题
当前输出文件名是:
All_values_{Start}_{End}_{Stepsize}
期望的输出是:
All_values_0_500001_1000
英文:
I am generating numbers in a specific range with a specific stepsize and saving to a .txt file. But I want to save the filename in a specific format. I present the current and expected output.
Start=0
End=500001
Stepsize=1000
for i in range(Start,End,Stepsize):
    #print(i)
    
    with open('All_values_{Start}_{End}_{Stepsize}.txt', 'a') as f:
        #writer = csv.writer(f)   
        #print(" =",i)
        f.writelines('\n')
        f.write(str(i))
The current output in terms of file naming is
All_values_{Start}_{End}_{Stepsize}
The expected output is
All_values_0_500001_1000
答案1
得分: 3
Use f-strings, just add an f to this line:
    with open(f'All_values_{Start}_{End}_{Stepsize}.txt', 'a') as f:
英文:
Use f-strings, just add an f to this line:
    with open(f'All_values_{Start}_{End}_{Stepsize}.txt', 'a') as f:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论