[英]Problem in saving specific file name in python 3
After running the following program in jupyter lab the saved filename is ('test_C0_s50_d', 1, 't', 2, '18_l1w')
, but I don't want parenthesis, comma, and spaces in the file name.在 jupyter 实验室中运行以下程序后,保存的文件名是
('test_C0_s50_d', 1, 't', 2, '18_l1w')
,但我不希望文件名中包含括号、逗号和空格。 I want something similar to test_C0_s50_d1t2_18_l1w
in the filename so that when changing i and j value I can find another name.我想要文件名中类似于
test_C0_s50_d1t2_18_l1w
东西,以便在更改 i 和 j 值时我可以找到另一个名称。
i = 1
j = 2
plt.scatter(1,2)
saving = 'test_C0_s50_d',i,'t',j,'18_l1w'
print(saving)
plt.savefig("{0}.png".format(saving))
plt.show()
One way of doing this is to create a list of strings you want to use then call join
function:一种方法是创建要使用的字符串列表,然后调用
join
函数:
i = 1
j = 2
prefix = "test_C0_s50_d"
names = [prefix, str(i), str(j)]
saving = "_".join(names)
您需要使用format
(或类似的)来获取您想要的数字:
saving = 'test_C0_s50_d{0}t{1}18_l1w'.format(i, j)
Use fast string (f string) which is easy also!使用快速字符串(f 字符串)也很容易!
i = 1
j = 2
filename = f'test_C0_s50_d{i}t{j}18_l1w'
plt.savefig(filename)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.