[英]How to pass varying number of arguments from tuple to string format in python?
[英]format string python with varying number of placeholders
def SumFunc(E,n):
write_path = open('somepath.text','a')
some function here evaluates the following...
c1 =
c2 =
c4 =
c4 =
write_path.write('{} {} {} {} {}\n'.format(E,c1,c2,c3,c4))
我有一个函数,取决于'E'和'n' ,其中'n'是最后的占位符数量,在这种情况下为5。对于不同的示例,我的'n'从4到100不等。
我的问题是如何格式化文件上的文字,使其适用于所有'n'格式。 像在n = 4中一样,它使用4 个占位符写入,而在n = 50中时,它将创建50个占位符并写入50个值。
谢谢
将您的值(Cs)存储在列表中,我们称它为c_list
然后以这种方式生成您的字符串:
string = ' '.join([str(x) for x in c_list]) + '\n'
write_path.write(string)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.